using System.Text.Json.Serialization;

namespace PSLauncher.Models;

public sealed class LicenseValidationResponse
{
    [JsonPropertyName("status")]
    public string Status { get; set; } = "invalid";

    [JsonPropertyName("licenseId")]
    public string? LicenseId { get; set; }

    [JsonPropertyName("ownerName")]
    public string? OwnerName { get; set; }

    [JsonPropertyName("issuedAt")]
    public DateTime? IssuedAt { get; set; }

    [JsonPropertyName("downloadEntitlementUntil")]
    public DateTime? DownloadEntitlementUntil { get; set; }

    [JsonPropertyName("maxMachines")]
    public int? MaxMachines { get; set; }

    [JsonPropertyName("serverTime")]
    public DateTime? ServerTime { get; set; }

    [JsonPropertyName("message")]
    public string? Message { get; set; }

    [JsonPropertyName("signature")]
    public string? Signature { get; set; }

    public bool IsValid => Status == "valid";
    public bool IsExpired => Status == "expired";

    public bool CanDownload(VersionManifest version)
    {
        if (!IsValid && !IsExpired) return false;
        if (DownloadEntitlementUntil is null) return false;
        if (version.MinLicenseDate is null) return true; // pas de minimum imposé
        return DownloadEntitlementUntil >= version.MinLicenseDate;
    }
}

public sealed class LicenseValidationRequest
{
    [JsonPropertyName("licenseKey")] public string LicenseKey { get; set; } = string.Empty;
    [JsonPropertyName("machineId")] public string MachineId { get; set; } = string.Empty;
    [JsonPropertyName("machineLabel")] public string? MachineLabel { get; set; }
    [JsonPropertyName("launcherVersion")] public string LauncherVersion { get; set; } = string.Empty;
}
