using ITNexusAgent.Models;
using Newtonsoft.Json;
using System.Net.Http;
using System.Text;

namespace ITNexusAgent.Services;

public class ApiService(string serverUrl, string agentKey)
{
    private readonly HttpClient _http = new() { Timeout = TimeSpan.FromSeconds(30) };
    private readonly string _baseUrl = serverUrl.TrimEnd('/');
    private readonly string _agentKey = agentKey;

    private HttpRequestMessage BuildRequest(HttpMethod method, string path, object? body = null)
    {
        var req = new HttpRequestMessage(method, $"{_baseUrl}{path}");
        req.Headers.Add("X-Agent-Key", _agentKey);
        if (body != null)
        {
            var json = JsonConvert.SerializeObject(body);
            req.Content = new StringContent(json, Encoding.UTF8, "application/json");
        }
        return req;
    }

    public async Task<CheckinResponse> CheckinAsync(CheckinPayload payload)
    {
        var req = BuildRequest(HttpMethod.Post, "/api/monitoring/checkin", payload);
        var resp = await _http.SendAsync(req);
        resp.EnsureSuccessStatusCode();
        var body = await resp.Content.ReadAsStringAsync();
        return JsonConvert.DeserializeObject<CheckinResponse>(body) ?? new CheckinResponse();
    }

    public async Task<List<Announcement>> PollAnnouncementsAsync(string hostname)
    {
        var req = BuildRequest(HttpMethod.Post, "/api/monitoring/announcements-poll",
            new { hostname });
        var resp = await _http.SendAsync(req);
        resp.EnsureSuccessStatusCode();
        var body = await resp.Content.ReadAsStringAsync();
        var result = JsonConvert.DeserializeAnonymousType(body, new { announcements = new List<Announcement>() });
        return result?.announcements ?? [];
    }

    public async Task AckAnnouncementAsync(int id, string hostname)
    {
        var req = BuildRequest(HttpMethod.Post, $"/api/announcements/{id}/ack-agent",
            new { hostname });
        var resp = await _http.SendAsync(req);
        resp.EnsureSuccessStatusCode();
    }

    public async Task ReportCommandResultAsync(CommandResult result)
    {
        var req = BuildRequest(HttpMethod.Post, "/api/patch/commands/result", result);
        var resp = await _http.SendAsync(req);
        resp.EnsureSuccessStatusCode();
    }

    public async Task<byte[]> DownloadAgentAsync()
    {
        var req = BuildRequest(HttpMethod.Get, "/api/monitoring/agent-script");
        var resp = await _http.SendAsync(req);
        return await resp.Content.ReadAsByteArrayAsync();
    }

    public async Task<byte[]> DownloadSetupAsync(string hostname)
    {
        var req = BuildRequest(HttpMethod.Get, $"/api/monitoring/agent-setup?hostname={Uri.EscapeDataString(hostname)}");
        var resp = await _http.SendAsync(req);
        resp.EnsureSuccessStatusCode();
        return await resp.Content.ReadAsByteArrayAsync();
    }

    public async Task SendMessageAsync(string hostname, string message)
    {
        var req = BuildRequest(HttpMethod.Post, "/api/monitoring/message",
            new { hostname, message });
        await _http.SendAsync(req);
    }

    public async Task SendRebootRequestAsync(string hostname)
    {
        var req = BuildRequest(HttpMethod.Post, "/api/monitoring/reboot-request",
            new { hostname });
        await _http.SendAsync(req);
    }
}
