using Brizco.Core.CoreServices.ReportServices.Commands;

namespace Brizco.Api.Controllers;

public class ReportController : ICarterModule
{
    public void AddRoutes(IEndpointRouteBuilder app)
    {
        var group = app.NewVersionedApi("Report")
            .MapGroup("api/report")
            .RequireAuthorization(builder => builder.AddAuthenticationSchemes("Bearer").RequireAuthenticatedUser());

        group.MapGet("task", GetTasksReportAsync)
            .WithDisplayName("Get Tasks Report")
            .RequireAuthorization(builder => builder.RequireClaim(CustomClaimType.Permission, ApplicationPermission.ManageReports))
            .HasApiVersion(1.0);

        group.MapGet("shift/plan/{shiftPLanId}", GetShiftPlanReportAsync)
            .WithDisplayName("Get ShiftPlan Report")
            .RequireAuthorization(builder => builder.RequireClaim(CustomClaimType.Permission, ApplicationPermission.ManageReports))
            .HasApiVersion(1.0);
    }

    public async Task<IResult> GetTasksReportAsync([FromServices]IMediator mediator,CancellationToken cancellationToken)
    {
        var file = await mediator.Send(new TaskReportCommand(),cancellationToken);
        string fileName = $"TaskReport_{DateTime.Now:yyyy-MM-dd}.xlsx";
        return TypedResults.File(file, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", fileName);
    }

    public async Task<IResult> GetShiftPlanReportAsync(Guid shiftPLanId,[FromServices] IMediator mediator, CancellationToken cancellationToken)
    {
        var file = await mediator.Send(new ShiftPlanReportCommand(ShiftPlanId: shiftPLanId), cancellationToken);
        string fileName = $"ShiftPlanReport_{DateTime.Now:yyyy-MM-dd}.xlsx";
        return TypedResults.File(file, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", fileName);
    }
}