#ifndef TIMER_H
#define TIMER_H

#include <string>
#include <chrono>
#include <iostream>
#include "../io/logger.h"

// Benchmark timer for tracking total stacking time
class BenchmarkTimer
{
    public:
        BenchmarkTimer() : m_start(std::chrono::high_resolution_clock::now()) {}

        // Reset the timer
        void reset()
        {
            m_start = std::chrono::high_resolution_clock::now();
        }

        // Get elapsed time in milliseconds
        long long getElapsedMs() const
        {
            auto now = std::chrono::high_resolution_clock::now();
            return std::chrono::duration_cast<std::chrono::milliseconds>(now - m_start).count();
        }

    private:
        std::chrono::time_point<std::chrono::high_resolution_clock> m_start;
};

// Timer class for performance measurements
class Timer
{
    public:
        // Static method to enable/disable all timers
        static void setEnabled(bool enabled)
        {
            s_enabled = enabled;
        }

        // Static method to check if timers are enabled
        static bool isEnabled()
        {
            return s_enabled;
        }

        // Get total execution time in milliseconds
        static long long getTotalExecutionTimeMs()
        {
            return s_totalTimer.getElapsedMs();
        }

        // Log the total execution time
        static void logTotalExecutionTime()
        {
            if (s_enabled)
            {
                auto totalTime = getTotalExecutionTimeMs();
                LOG_INFO("Total execution time: " + std::to_string(totalTime) + " ms");
            }
        }

        Timer(const std::string &name)
            : m_name(name),
              m_start(std::chrono::high_resolution_clock::now()) {}

        ~Timer()
        {
            if (s_enabled)
            {
                auto end = std::chrono::high_resolution_clock::now();
                auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - m_start).count();

                // Only log if verbose mode is enabled
                if (Logger::getInstance().getVerboseMode())
                {
                    LOG_INFO("Timer [" + m_name + "]: " + std::to_string(duration) + " ms");
                }
            }
        }

        // Get elapsed time in milliseconds without destroying the timer
        long long getElapsedMs() const
        {
            auto now = std::chrono::high_resolution_clock::now();
            return std::chrono::duration_cast<std::chrono::milliseconds>(now - m_start).count();
        }

    private:
        std::string m_name;
        std::chrono::time_point<std::chrono::high_resolution_clock> m_start;

        // Static variable to control whether timers are enabled
        static bool s_enabled;

        // Static timer for tracking total execution time
        static BenchmarkTimer s_totalTimer;
};

// Static variables defined in timer.cpp

// Note: The TIME_FUNCTION macro is defined in stacker_common.tpp

#endif // TIMER_H
