using FastEndpoints;
using TaxBaik.Application.Services;

namespace TaxBaik.Web.Endpoints.ConsultingActivity;

public class CreatReq { public int ClientId { get; set; } public string? ActivityType { get; set; } public DateTime ActivityDate { get; set; } public string? Notes { get; set; } }
public class UpdateReq { public string? Outcome { get; set; } public DateTime? NextFollowupDate { get; set; } }
public class Resp { public List<object> Data { get; set; } = []; }
public class IdResp { public int Id { get; set; } }
public class DateQry { public DateTime FromDate { get; set; } }

public class CreateEp : Endpoint<CreatReq, IdResp>
{
    readonly ConsultingActivityService _s;
    public CreateEp(ConsultingActivityService s) => _s = s;
    public override void Configure() { Post("/api/consultingactivity"); Policies("Bearer"); }
    public override async Task HandleAsync(CreatReq r, CancellationToken ct)
    {
        if (string.IsNullOrWhiteSpace(r.ActivityType) || string.IsNullOrWhiteSpace(r.Notes))
            ThrowError("활동 유형과 메모가 필요합니다.", statusCode: 400);
        var id = await _s.CreateAsync(r.ClientId, r.ActivityType, r.ActivityDate, r.Notes);
        await SendAsync(new IdResp { Id = id }, 201, cancellation: ct);
    }
}

public class GetAllEp : Endpoint<EmptyRequest, Resp>
{
    readonly ConsultingActivityService _s;
    public GetAllEp(ConsultingActivityService s) => _s = s;
    public override void Configure() { Get("/api/consultingactivity"); Policies("Bearer"); }
    public override async Task HandleAsync(EmptyRequest _, CancellationToken ct)
    {
        var items = await _s.GetAllAsync();
        await SendAsync(new Resp { Data = items.Cast<object>().ToList() }, 200, cancellation: ct);
    }
}

public class GetByClientEp : Endpoint<EmptyRequest, Resp>
{
    readonly ConsultingActivityService _s;
    public GetByClientEp(ConsultingActivityService s) => _s = s;
    public override void Configure() { Get("/api/consultingactivity/client/{clientId}"); Policies("Bearer"); }
    public override async Task HandleAsync(EmptyRequest _, CancellationToken ct)
    {
        var items = await _s.GetByClientIdAsync(Route<int>("clientId"));
        await SendAsync(new Resp { Data = items.Cast<object>().ToList() }, 200, cancellation: ct);
    }
}

public class GetPendingFollowupsEp : Endpoint<EmptyRequest, Resp>
{
    readonly ConsultingActivityService _s;
    public GetPendingFollowupsEp(ConsultingActivityService s) => _s = s;
    public override void Configure() { Get("/api/consultingactivity/pending-followups"); Policies("Bearer"); }
    public override async Task HandleAsync(EmptyRequest _, CancellationToken ct)
    {
        var items = await _s.GetPendingFollowupsAsync();
        await SendAsync(new Resp { Data = items.Cast<object>().ToList() }, 200, cancellation: ct);
    }
}

public class GetConsultantActivityEp : Endpoint<DateQry, Resp>
{
    readonly ConsultingActivityService _s;
    public GetConsultantActivityEp(ConsultingActivityService s) => _s = s;
    public override void Configure() { Get("/api/consultingactivity/consultant/{consultantId}"); Policies("Bearer"); }
    public override async Task HandleAsync(DateQry q, CancellationToken ct)
    {
        var items = await _s.GetConsultantActivityAsync(Route<int>("consultantId"), q.FromDate);
        await SendAsync(new Resp { Data = items.Cast<object>().ToList() }, 200, cancellation: ct);
    }
}

public class UpdateEp : Endpoint<UpdateReq, object>
{
    readonly ConsultingActivityService _s;
    public UpdateEp(ConsultingActivityService s) => _s = s;
    public override void Configure() { Put("/api/consultingactivity/{id}"); Policies("Bearer"); }
    public override async Task HandleAsync(UpdateReq r, CancellationToken ct)
    {
        await _s.UpdateAsync(Route<int>("id"), r.Outcome, r.NextFollowupDate);
        await SendAsync(new { message = "업데이트 완료" }, 200, cancellation: ct);
    }
}
