using Brizco.Domain.Enums;

namespace Brizco.Api.Controllers;

public class ShiftPlanController : ICarterModule
{
    public void AddRoutes(IEndpointRouteBuilder app)
    {

        var group = app.NewVersionedApi("ShiftPlan")
            .MapGroup($"api/shift/plan")
            .RequireAuthorization(builder => builder.AddAuthenticationSchemes("Bearer").RequireAuthenticatedUser());

        group.MapGet("", GetAllAsync)
            .WithDisplayName("GetShiftPlans")
            .RequireAuthorization(builder => builder.RequireClaim(CustomClaimType.Permission, ApplicationPermission.ManageShiftPlans, ApplicationPermission.ViewShiftPlans))
            .HasApiVersion(1.0);

        group.MapGet("{id:guid}", GetAsync)
            .WithDisplayName("GetShiftPlan")
            .RequireAuthorization(builder => builder.RequireClaim(CustomClaimType.Permission, ApplicationPermission.ManageShiftPlans, ApplicationPermission.ViewShiftPlans))
            .HasApiVersion(1.0);

        group.MapPost("{id:guid}/complete", CompletePlanAsync)
            .WithDisplayName("DoneActivity")
            .RequireAuthorization(builder => builder.RequireClaim(CustomClaimType.Permission, ApplicationPermission.ManageShiftPlans))
            .HasApiVersion(1.0);

        group.MapPost("", Post)
            .RequireAuthorization(builder => builder.RequireClaim(CustomClaimType.Permission, ApplicationPermission.ManageShiftPlans))
            .HasApiVersion(1.0);

        group.MapPut("", Put)
            .RequireAuthorization(builder => builder.RequireClaim(CustomClaimType.Permission, ApplicationPermission.ManageShiftPlans))
            .HasApiVersion(1.0);

        group.MapDelete("{id:guid}", Delete)
        .HasApiVersion(1.0);

    }

    public async Task<IResult> CompletePlanAsync(Guid id,[FromBody] CompleteShiftPlanRequestDto requestDtos, [FromServices] IShiftPlanService shiftPlanService, CancellationToken cancellationToken)
        => TypedResults.Ok(await shiftPlanService.CompleteShiftPlanAsync(id,requestDtos, cancellationToken));


    // GET:Get All Entity
    public async Task<IResult> GetAllAsync([FromQuery] int page, [FromQuery] long? selectedDate, [FromQuery] DateTimeQueryFilter? dateFilter , ISender sender, CancellationToken cancellationToken)
        => TypedResults.Ok(await sender.Send(new GetShiftPlansQuery(page , selectedDate ?? 0 , DateTimeQueryFilter: dateFilter), cancellationToken));

    // GET:Get An Entity By Id
    public async Task<IResult> GetAsync(Guid id, ISender sender, CancellationToken cancellationToken)
        => TypedResults.Ok(await sender.Send(new GetShiftPlanQuery(id),cancellationToken));

    // POST:Create Entity
    public async Task<IResult> Post([FromBody] CreateShiftPlanCommand ent, IShiftPlanService shiftPlanService, CancellationToken cancellationToken)
        => TypedResults.Ok(await shiftPlanService.CreateAsync(ent, cancellationToken));

    // PUT:Update Entity
    public async Task<IResult> Put([FromBody] UpdateShiftPlanCommand ent, IShiftPlanService shiftPlanService, CancellationToken cancellationToken)
        => TypedResults.Ok(await shiftPlanService.UpdateAsync(ent, cancellationToken));

    // DELETE:Delete Entity
    public async Task<IResult> Delete(Guid id, ISender mediator, CancellationToken cancellationToken)
        => TypedResults.Ok(await mediator.Send(new DeleteShiftPlanCommand(id), cancellationToken));
}