namespace Brizco.Api.Controllers;

public class RoutineController : ICarterModule
{
    public virtual void AddRoutes(IEndpointRouteBuilder app)
    {
        var group = app.NewVersionedApi("Routine")
            .MapGroup($"api/routine")
            .RequireAuthorization(builder => builder.AddAuthenticationSchemes("Bearer").RequireAuthenticatedUser());

        group.MapGet("", GetAllAsync)
            .WithDisplayName("GetAllRoutines")
            .RequireAuthorization(builder => builder.RequireClaim(CustomClaimType.Permission, ApplicationPermission.ManageRoutines,ApplicationPermission.ViewRoutines))
            .HasApiVersion(1.0);

        group.MapGet("{id}/shift", GetShiftsAsync)
            .WithDisplayName("GetRoutineShifts")
            .RequireAuthorization(builder => builder.RequireClaim(CustomClaimType.Permission, ApplicationPermission.ManageRoutines, ApplicationPermission.ViewRoutines))
            .HasApiVersion(1.0);

        group.MapGet("{id}", GetAsync)
            .WithDisplayName("GetRoutine")
            .RequireAuthorization(builder => builder.RequireClaim(CustomClaimType.Permission, ApplicationPermission.ManageRoutines))
            .HasApiVersion(1.0);

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

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

        group.MapDelete("{id}", Delete)
            .RequireAuthorization(builder => builder.RequireClaim(CustomClaimType.Permission, ApplicationPermission.ManageRoutines))
            .HasApiVersion(1.0);
    }

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

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

    // GET:Get Shifts By Id
    public async Task<IResult> GetShiftsAsync(Guid id, [FromQuery]long? selectedDate, ISender sender, CancellationToken cancellationToken)
        => TypedResults.Ok(await sender.Send(new GetRoutineShiftsQuery(id, selectedDate ?? 0), cancellationToken));

    // POST:Create Entity
    public async Task<IResult> Post([FromBody] CreateRoutineCommand ent, ISender mediator, CancellationToken cancellationToken)
        => TypedResults.Ok(await mediator.Send(ent, cancellationToken));

    // PUT:Update Entity
    public async Task<IResult> Put([FromBody] UpdateRoutineCommand ent, ISender mediator, CancellationToken cancellationToken)
        => TypedResults.Ok(await mediator.Send(ent, cancellationToken));

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