namespace DocuMed.Repository.Handlers.Hospitals;

public class UpdateHospitalCommandHandler(IRepositoryWrapper repositoryWrapper) : IRequestHandler<UpdateHospitalCommand, Guid>
{

    public async Task<Guid> Handle(UpdateHospitalCommand request, CancellationToken cancellationToken)
    {
        var ent = await repositoryWrapper.SetRepository<Hospital>().TableNoTracking
            .FirstOrDefaultAsync(h => h.Id == request.Id, cancellationToken);
        if (ent == null)
            throw new BaseApiException(ApiResultStatusCode.NotFound, "Hospital not found");

        var newEnt = Hospital.Create(request.Name, request.Detail, request.Address, request.UniversityId);
        newEnt.Id = ent.Id;
        newEnt.CreatedAt = ent.CreatedAt;
        newEnt.CreatedBy = ent.CreatedBy;
        repositoryWrapper.SetRepository<Hospital>().Update(newEnt);
        await repositoryWrapper.SaveChangesAsync(cancellationToken);

        return newEnt.Id;

    }
}