using System.Text.Json;
using System.Text.Json.Serialization;

namespace Inspectron.Epson.Templates.Configuration;

public class TemplateConfiguration
{
    [JsonPropertyName("assignments")]
    public List<TemplateAssignmentJson> Assignments { get; set; } = new();

    [JsonPropertyName("fallbackTemplate")]
    public string FallbackTemplate { get; set; } = "fallback.template";

    public static TemplateConfiguration Load(string jsonPath)
    {
        var json = File.ReadAllText(jsonPath);
        return JsonSerializer.Deserialize<TemplateConfiguration>(json)
               ?? new TemplateConfiguration();
    }

    public static TemplateConfiguration LoadFromJson(string json)
    {
        return JsonSerializer.Deserialize<TemplateConfiguration>(json)
               ?? new TemplateConfiguration();
    }

    public IEnumerable<TemplateAssignment> GetAssignments()
    {
        return Assignments.Select(a => new TemplateAssignment(
            a.ReceiptType,
            a.ProfileId,
            a.TemplatePath));
    }
}

public class TemplateAssignmentJson
{
    [JsonPropertyName("receiptType")]
    public int ReceiptType { get; set; }

    [JsonPropertyName("profileId")]
    public string? ProfileId { get; set; }

    [JsonPropertyName("template")]
    public string TemplatePath { get; set; } = string.Empty;
}
