using Microsoft.IdentityModel.Tokens;

namespace DocuMed.Api.Controllers;
public class SectionController : ICarterModule
{
    public virtual void AddRoutes(IEndpointRouteBuilder app)
    {
        var group = app.NewVersionedApi("Section").MapGroup($"api/section");

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

        group.MapGet("University/{universityId}", GetAllByUniversityAsync)
            .WithDisplayName("GetAllByUniversityId")
            .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, IRepositoryWrapper repositoryWrapper,  ICurrentUserService currentUserService, CancellationToken cancellationToken)
    {
        return TypedResults.Ok(await repositoryWrapper.SetRepository<Section>().TableNoTracking
            .Select(SectionMapper.ProjectToSDto).ToListAsync(cancellationToken));
    }

    // GET:Get All Entity
    public virtual async Task<IResult> GetAllByUniversityAsync(Guid universityId, IRepositoryWrapper repositoryWrapper, ICurrentUserService currentUserService, CancellationToken cancellationToken)
    {
        return TypedResults.Ok(await repositoryWrapper.SetRepository<Section>().TableNoTracking
            .Where(s => s.UniversityId == universityId)
            .Select(SectionMapper.ProjectToSDto).ToListAsync(cancellationToken));
    }

    // GET:Get An Entity By Id
    public async Task<IResult> GetAsync(Guid id, IRepositoryWrapper repositoryWrapper, CancellationToken cancellationToken)
        => TypedResults.Ok(await repositoryWrapper.SetRepository<Section>().GetByIdAsync(cancellationToken, id));


    // POST:Add New Entity
    public virtual async Task<IResult> Post([FromBody] SectionSDto dto, IRepositoryWrapper repositoryWrapper, CancellationToken cancellationToken)
    {
        var ent = Section.Create(dto.Name,dto.Detail,dto.UniversityId);
        repositoryWrapper.SetRepository<Section>().Add(ent);
        await repositoryWrapper.SaveChangesAsync(cancellationToken);
        return TypedResults.Ok(ent);
    }

    // PUT:Update Entity
    public virtual async Task<IResult> Put([FromBody] SectionSDto dto, IRepositoryWrapper repositoryWrapper, CancellationToken cancellationToken)
    {
        var ent = Section.Create(dto.Name,dto.Detail, dto.UniversityId);
        ent.Id = dto.Id;
        repositoryWrapper.SetRepository<Section>().Update(ent);
        await repositoryWrapper.SaveChangesAsync(cancellationToken);
        return TypedResults.Ok();
    }

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