// ServiceBase.h - Modernized C++ Windows Service Header
#pragma once

#include <windows.h>
#include <string>
#include <thread>
#include <atomic>

class CServiceBase
{
public:
    // Register the service with the SCM.
    static BOOL Run(CServiceBase& service);

    // Constructor with service capabilities.
    CServiceBase(const std::wstring& serviceName,
        BOOL canStop = TRUE,
        BOOL canShutdown = TRUE,
        BOOL canPauseContinue = FALSE);

    virtual ~CServiceBase();

    // Stop the service.
    void Stop();

protected:
    // Overridable lifecycle methods.
    virtual void OnStart(DWORD argc, LPWSTR* argv);
    virtual void OnStop();
    virtual void OnPause();
    virtual void OnContinue();
    virtual void OnShutdown();

    // Update service status.
    void SetServiceStatus(DWORD currentState,
        DWORD win32ExitCode = NO_ERROR,
        DWORD waitHint = 0);

    // Logging utilities.
    void WriteEventLogEntry(const std::wstring& message, WORD type);
    void WriteErrorLogEntry(const std::wstring& function, DWORD error = GetLastError());

private:
    // Internal control handlers.
    static void WINAPI ServiceMain(DWORD argc, LPWSTR* argv);
    static void WINAPI ServiceCtrlHandler(DWORD ctrlCode);

    void Start(DWORD argc, LPWSTR* argv);
    void Pause();
    void Continue();
    void Shutdown();

    static CServiceBase* s_service; // Singleton instance

    std::wstring m_name;            // Service name
    SERVICE_STATUS m_status{};      // Current service status
    SERVICE_STATUS_HANDLE m_statusHandle = nullptr; // Handle to SCM
};
