using Brizco.Common.Models.Claims;
using Brizco.Domain.Enums;

namespace Brizco.Api.Controllers;

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

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

        group.MapGet("", GetAllAsync)
            .WithDisplayName("GetActivities")
            .RequireAuthorization(builder=>builder.RequireClaim(CustomClaimType.Permission,ApplicationPermission.ViewActivities, ApplicationPermission.ManageActivities))
            .HasApiVersion(1.0);

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

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

        group.MapPost("complete", CompleteActivityAsync)
            .WithDisplayName("DoneActivity")
            .RequireAuthorization(builder => builder.RequireClaim(CustomClaimType.Permission, ApplicationPermission.ChangeActivityStatus))
            .HasApiVersion(1.0);

        group.MapPost("undone/{id:guid}", UnDoneActivityAsync)
            .WithDisplayName("UnDoneActivity")
            .RequireAuthorization(builder => builder.RequireClaim(CustomClaimType.Permission, ApplicationPermission.ChangeActivityStatus))
            .HasApiVersion(1.0);

        //group.MapPost("", Post)
        //    .AllowAnonymous()
        //    .HasApiVersion(1.0);

        //group.MapPut("", Put)
        //    .HasApiVersion(1.0);

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

    }


    // GET:Get All Entity
    public async Task<IResult> GetAllAsync([FromQuery] int page, [FromQuery] DateQueryFilter? dateQueryFilter, [FromQuery] long? selectedDate, [FromQuery] Guid? selectedShift, ISender sender, CancellationToken cancellationToken)
        => TypedResults.Ok(await sender.Send(new GetActivitiesQuery(Page: page, SelectedDate: selectedDate ?? 0 , SelectedShift: selectedShift ?? default), cancellationToken));

    // GET:Get An Entity By Id
    public async Task<IResult> GetAsync(Guid id, IActivityService activityService, CancellationToken cancellationToken)
        => TypedResults.Ok(await activityService.DoneActivityAsync(id,cancellationToken));

    public async Task<IResult> CompleteActivityAsync([FromBody]List<CompleteActivityRequestDto> requestDtos, [FromServices] IActivityService activityService, CancellationToken cancellationToken)
        => TypedResults.Ok(await activityService.CompleteActivitiesAsync(requestDtos, cancellationToken));

    public async Task<IResult> DoneActivityAsync(Guid id, [FromServices] IActivityService activityService, CancellationToken cancellationToken)
        => TypedResults.Ok(await activityService.DoneActivityAsync(id, cancellationToken));

    public async Task<IResult> UnDoneActivityAsync(Guid id,[FromQuery]string undoneReason, [FromServices]IActivityService activityService, CancellationToken cancellationToken)
        => TypedResults.Ok(await activityService.UnDoneActivityAsync(id,undoneReason, cancellationToken));

    //// POST:Create Entity
    //public async Task<IResult> Post([FromQuery]Guid shiftPlanId,IActivityService activityService, CancellationToken cancellationToken)
    //    => TypedResults.Ok(await activityService.CreateActivitiesByShiftPlan(shiftPlanId,cancellationToken));

    //// PUT:Update Entity
    //public async Task<IResult> Put([FromBody] UpdateActivityCommand 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 DeleteActivityCommand(id), cancellationToken));
}