namespace TaxBaik.Application.Services;

using TaxBaik.Domain.Entities;
using TaxBaik.Domain.Interfaces;

public class TaxFilingService(ITaxFilingRepository repository)
{
    public static readonly string[] Statuses =
        ["pending", "filed", "overdue"];

    public static readonly Dictionary<string, string> StatusLabels = new()
    {
        ["pending"] = "신고 예정",
        ["filed"] = "신고 완료",
        ["overdue"] = "기한 초과",
    };

    public async Task<IEnumerable<TaxFiling>> GetByClientIdAsync(int clientId, CancellationToken ct = default) =>
        await repository.GetByClientIdAsync(clientId, ct);

    public async Task<IEnumerable<TaxFiling>> GetUpcomingAsync(int daysAhead = 30, CancellationToken ct = default) =>
        await repository.GetUpcomingAsync(daysAhead, ct);

    public async Task<TaxFiling?> GetByIdAsync(int id, CancellationToken ct = default) =>
        await repository.GetByIdAsync(id, ct);

    public async Task<int> CreateAsync(TaxFiling filing, CancellationToken ct = default)
    {
        if (string.IsNullOrWhiteSpace(filing.FilingType))
            throw new ValidationException("신고 유형을 선택하세요.");
        if (filing.ClientId <= 0)
            throw new ValidationException("고객을 선택하세요.");
        if (filing.DueDate == default)
            throw new ValidationException("신고 기한을 입력하세요.");
        return await repository.CreateAsync(filing, ct);
    }

    public async Task UpdateAsync(TaxFiling filing, CancellationToken ct = default)
    {
        if (string.IsNullOrWhiteSpace(filing.FilingType))
            throw new ValidationException("신고 유형을 선택하세요.");
        await repository.UpdateAsync(filing, ct);
    }

    public async Task DeleteAsync(int id, CancellationToken ct = default) =>
        await repository.DeleteAsync(id, ct);
}
