using DocuMed.Domain.CommandQueries.Commands;
using DocuMed.Domain.Entities.MedicalHistory;
using MediatR;

namespace DocuMed.Api.Controllers;
public class MedicalHistoryController : ICarterModule
{
    public virtual void AddRoutes(IEndpointRouteBuilder app)
    {
        var group = app.NewVersionedApi("MedicalHistory").MapGroup($"api/medicalhistory")
            .RequireAuthorization(builder => builder.AddAuthenticationSchemes("Bearer").RequireAuthenticatedUser());

        group.MapGet("", GetAllAsync)
            .WithDisplayName("GetAll")
            .HasApiVersion(1.0);

        group.MapGet("/filter", GetAllByFilterAsync)
            .WithDisplayName("GetAllByFilter")
            .HasApiVersion(1.0);

        group.MapGet("{id}", GetAsync)
            .WithDisplayName("GetOne")
            .HasApiVersion(1.0);

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

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

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



    // GET:Get All Entity
    public virtual async Task<IResult> GetAllByFilterAsync([FromQuery]DayQueryFilter dayQuery,[FromQuery] int page, IMedicalHistoryRepository repository, CancellationToken cancellationToken)
        => TypedResults.Ok(await repository.GetMedicalHistoriesByFilterAsync(dayQuery, page, cancellationToken));

    // GET:Get All Entity
    public virtual async Task<IResult> GetAllAsync([FromQuery] int page, IMedicalHistoryRepository repository, CancellationToken cancellationToken)
        => TypedResults.Ok(await repository.GetMedicalHistoriesAsync(page, cancellationToken));

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

    // POST:Add New Entity
    public virtual async Task<IResult> Post([FromBody] CreateMedicalHistoryCommand dto, IMediator service, ICurrentUserService currentUserService, CancellationToken cancellationToken)
        => TypedResults.Ok(await service.Send(dto, cancellationToken));

    // PUT:Update Entity
    public virtual async Task<IResult> Put([FromBody] UpdateMedicalHistoryCommand dto, IMediator service, ICurrentUserService currentUserService, CancellationToken cancellationToken)
        => TypedResults.Ok(await service.Send(dto, cancellationToken));

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