using DocuMed.Domain.Entities.MedicalHistory;

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("{id}", GetAsync)
            .WithDisplayName("GetOne")
            .HasApiVersion(1.0);

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

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

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


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

    // GET:Get An Entity By Id
    public async Task<IResult> GetAsync(Guid id, IMedicalHistoryRepository repository, CancellationToken cancellationToken)
    {

        return TypedResults.Ok(await repository.GetMedicalHistoryAsync(id, cancellationToken));
    }

    // POST:Add New Entity
    public virtual async Task<IResult> Post([FromBody] MedicalHistoryLDto dto, IMedicalHistoryService service, ICurrentUserService currentUserService, CancellationToken cancellationToken)
    {
        return TypedResults.Ok(await service.AddAsync(dto, cancellationToken));
    }

    // PUT:Update Entity
    public virtual async Task<IResult> Put([FromBody] MedicalHistoryLDto dto, IMedicalHistoryService service, ICurrentUserService currentUserService, CancellationToken cancellationToken)
    {
        return TypedResults.Ok(await service.EditAsync(dto, cancellationToken));
    }

    // DELETE:Delete Entity
    public virtual async Task<IResult> Delete(int id, IRepositoryWrapper repositoryWrapper, CancellationToken cancellationToken)
    {
        var ent = await repositoryWrapper.SetRepository<MedicalHistory>().GetByIdAsync(cancellationToken, id);
        repositoryWrapper.SetRepository<MedicalHistory>().Delete(ent);
        await repositoryWrapper.SaveChangesAsync(cancellationToken);
        return TypedResults.Ok();
    }
}
