// update_service.h - Background app update checker
// Milestone 10: Device-Side App Management
#pragma once

#include "app_manager.h"

#include <string>
#include <vector>
#include <thread>
#include <atomic>
#include <chrono>
#include <functional>
#include <mutex>

namespace mosis {

// Information about an available update
struct UpdateInfo {
    std::string package_id;
    std::string name;
    std::string current_version;
    std::string new_version;
    int current_version_code = 0;
    int new_version_code = 0;
    std::string download_url;
    std::string signature;
    int64_t download_size = 0;
    std::string release_notes;
    bool is_critical = false;
};

// Callback for update events
using UpdateCheckCallback = std::function<void(const std::vector<UpdateInfo>& updates)>;
using UpdateAppliedCallback = std::function<void(const std::string& package_id, bool success)>;

class UpdateService {
public:
    UpdateService(AppManager* app_manager, const std::string& api_base);
    ~UpdateService();

    // Prevent copying
    UpdateService(const UpdateService&) = delete;
    UpdateService& operator=(const UpdateService&) = delete;

    // Start background update checking
    void Start(std::chrono::hours interval = std::chrono::hours(24));
    void Stop();
    bool IsRunning() const { return m_running; }

    // Manual update check
    std::vector<UpdateInfo> CheckForUpdates();
    void CheckForUpdatesAsync(UpdateCheckCallback callback);

    // Download and install update
    bool ApplyUpdate(const std::string& package_id, ProgressCallback callback);
    void ApplyUpdateAsync(const std::string& package_id, UpdateAppliedCallback callback);

    // Apply all available updates
    void ApplyAllUpdates(UpdateAppliedCallback callback);

    // Get pending updates (from last check)
    std::vector<UpdateInfo> GetPendingUpdates() const;
    bool HasPendingUpdates() const;
    size_t GetPendingUpdateCount() const;

    // Settings
    void SetAutoUpdate(bool enabled) { m_auto_update = enabled; }
    bool GetAutoUpdate() const { return m_auto_update; }

    void SetWifiOnly(bool wifi_only) { m_wifi_only = wifi_only; }
    bool GetWifiOnly() const { return m_wifi_only; }

    void SetCheckInterval(std::chrono::hours interval) { m_check_interval = interval; }
    std::chrono::hours GetCheckInterval() const { return m_check_interval; }

    // Callbacks
    void SetOnUpdatesAvailable(UpdateCheckCallback callback) { m_on_updates_available = callback; }

    // Get last check time
    std::chrono::system_clock::time_point GetLastCheckTime() const { return m_last_check; }

private:
    // Background thread function
    void CheckLoop();

    // HTTP request helpers
    std::string HttpGet(const std::string& url);
    bool IsOnWifi() const;

    AppManager* m_app_manager;
    std::string m_api_base;

    std::thread m_check_thread;
    std::atomic<bool> m_running{false};
    std::atomic<bool> m_stop_requested{false};

    std::chrono::hours m_check_interval{24};
    std::chrono::system_clock::time_point m_last_check;

    bool m_auto_update = false;
    bool m_wifi_only = true;

    mutable std::mutex m_mutex;
    std::vector<UpdateInfo> m_pending_updates;

    UpdateCheckCallback m_on_updates_available;
};

} // namespace mosis
