using FastEndpoints;
using TaxBaik.Application.Services;

namespace TaxBaik.Web.Endpoints.RevenueTracking;

public class CreateRequest
{
    public int ClientId { get; set; }
    public string InvoiceNumber { get; set; } = "";
    public DateTime InvoiceDate { get; set; }
    public decimal Amount { get; set; }
    public string? ServiceType { get; set; }
    public DateTime? DueDate { get; set; }
}

public class MarkPaidRequest { public DateTime PaymentDate { get; set; } }
public class ListResp { public List<object> Data { get; set; } = []; }
public class IdResp { public int Id { get; set; } }
public class TotalResp { public decimal Total { get; set; } public DateTime StartDate { get; set; } public DateTime EndDate { get; set; } }
public class MonthlyQry { public int Year { get; set; } public int Month { get; set; } }
public class DateRangeQry { public DateTime StartDate { get; set; } public DateTime EndDate { get; set; } }

public class CreateEp : Endpoint<CreateRequest, IdResp>
{
    readonly RevenueTrackingService _svc;
    public CreateEp(RevenueTrackingService svc) => _svc = svc;
    public override void Configure() { Post("/api/revenue-tracking"); Policies("Bearer"); }
    public override async Task HandleAsync(CreateRequest r, CancellationToken ct)
    {
        var id = await _svc.CreateAsync(r.ClientId, r.InvoiceNumber, r.InvoiceDate, r.Amount, r.ServiceType, r.DueDate, ct);
        await SendAsync(new IdResp { Id = id }, 201, cancellation: ct);
    }
}

public class GetAllEp : Endpoint<EmptyRequest, ListResp>
{
    readonly RevenueTrackingService _svc;
    public GetAllEp(RevenueTrackingService svc) => _svc = svc;
    public override void Configure() { Get("/api/revenue-tracking"); Policies("Bearer"); }
    public override async Task HandleAsync(EmptyRequest _, CancellationToken ct)
    {
        var revs = await _svc.GetAllAsync(ct);
        await SendAsync(new ListResp { Data = revs.Cast<object>().ToList() }, 200, cancellation: ct);
    }
}

public class GetByClientEp : Endpoint<EmptyRequest, ListResp>
{
    readonly RevenueTrackingService _svc;
    public GetByClientEp(RevenueTrackingService svc) => _svc = svc;
    public override void Configure() { Get("/api/revenue-tracking/client/{clientId}"); Policies("Bearer"); }
    public override async Task HandleAsync(EmptyRequest _, CancellationToken ct)
    {
        var id = Route<int>("clientId");
        var revs = await _svc.GetByClientIdAsync(id, ct);
        await SendAsync(new ListResp { Data = revs.Cast<object>().ToList() }, 200, cancellation: ct);
    }
}

public class GetPendingEp : Endpoint<EmptyRequest, ListResp>
{
    readonly RevenueTrackingService _svc;
    public GetPendingEp(RevenueTrackingService svc) => _svc = svc;
    public override void Configure() { Get("/api/revenue-tracking/pending"); Policies("Bearer"); }
    public override async Task HandleAsync(EmptyRequest _, CancellationToken ct)
    {
        var revs = await _svc.GetPendingPaymentsAsync(ct);
        await SendAsync(new ListResp { Data = revs.Cast<object>().ToList() }, 200, cancellation: ct);
    }
}

public class GetMonthlyEp : Endpoint<MonthlyQry, ListResp>
{
    readonly RevenueTrackingService _svc;
    public GetMonthlyEp(RevenueTrackingService svc) => _svc = svc;
    public override void Configure() { Get("/api/revenue-tracking/monthly"); Policies("Bearer"); }
    public override async Task HandleAsync(MonthlyQry r, CancellationToken ct)
    {
        var monthDate = new DateTime(r.Year, r.Month, 1);
        var revs = await _svc.GetMonthlyRevenueAsync(monthDate, ct);
        await SendAsync(new ListResp { Data = revs.Cast<object>().ToList() }, 200, cancellation: ct);
    }
}

public class GetTotalEp : Endpoint<DateRangeQry, TotalResp>
{
    readonly RevenueTrackingService _svc;
    public GetTotalEp(RevenueTrackingService svc) => _svc = svc;
    public override void Configure() { Get("/api/revenue-tracking/total"); Policies("Bearer"); }
    public override async Task HandleAsync(DateRangeQry r, CancellationToken ct)
    {
        var total = await _svc.GetTotalRevenueAsync(r.StartDate, r.EndDate, ct);
        await SendAsync(new TotalResp { Total = total, StartDate = r.StartDate, EndDate = r.EndDate }, 200, cancellation: ct);
    }
}

public class MarkPaidEp : Endpoint<MarkPaidRequest, object>
{
    readonly RevenueTrackingService _svc;
    public MarkPaidEp(RevenueTrackingService svc) => _svc = svc;
    public override void Configure() { Put("/api/revenue-tracking/{id}/paid"); Policies("Bearer"); }
    public override async Task HandleAsync(MarkPaidRequest r, CancellationToken ct)
    {
        var id = Route<int>("id");
        await _svc.MarkPaidAsync(id, r.PaymentDate, ct);
        await SendAsync(new { message = "결제가 완료됨으로 표시되었습니다." }, 200, cancellation: ct);
    }
}
