using Application.Services.Database;
using Application.Services.HashingAlgorithms;

namespace Application.Endpoints.Patients.Profile;

public class PatientProfileHandler
{
    private readonly IHashingAlgorithms _hashingAlgorithms;
    private readonly IPatientRepository _patientRepository;

    public PatientProfileHandler(IPatientRepository patientRepository, IHashingAlgorithms hashingAlgorithms)
    {
        _patientRepository = patientRepository;
        _hashingAlgorithms = hashingAlgorithms;
    }

    public async Task<BaseResponse> HandleGet(Guid id)
    {
        var patient = await _patientRepository.GetByIdAsync(id).ConfigureAwait(false);
        if (patient != null)
            return new BaseResponse
            {
                StatusCode = HttpStatusCodes.OK,
                Message = $"Retrieved patient with id: {id}",
                Data = patient
            };

        return new BaseResponse
        {
            StatusCode = HttpStatusCodes.NotFound,
            Message = $"Patient with id: {id} not found",
            Data = null
        };
    }

    public async Task<BaseResponse> HandleGetAll()
    {
        var patients = await _patientRepository.GetAllAsync().ConfigureAwait(false);
        if (patients.Any())
            return new BaseResponse
            {
                StatusCode = HttpStatusCodes.OK,
                Message = "Retrieved patients",
                Data = patients.ToList()
            };

        return new BaseResponse
        {
            StatusCode = HttpStatusCodes.NoContent,
            Message = "Patients not found",
            Data = null
        };
    }

    public async Task<BaseResponse> HandleUpdate(PatientProfileDto updateDto)
    {
        var validation = new PatientProfileValidation(_patientRepository);
        var validationResult = await validation.ValidateAsync(updateDto);

        if (!validationResult.IsValid)
        {
            var firstError = validationResult.Errors.FirstOrDefault();
            var errorCode = int.TryParse(firstError.ErrorCode, out var code) ? code : -1;
            var errorMessage = firstError.ErrorMessage;

            return new BaseResponse
            {
                StatusCode = errorCode,
                Message = errorMessage,
                Data = null
            };
        }

        var patientToUpdate = await _patientRepository.GetByIdAsync(updateDto.Id);

        patientToUpdate.SetEmail(updateDto.Email);
        patientToUpdate.SetPassword(_hashingAlgorithms.SHA256Algorithm(updateDto.Password));
        patientToUpdate.SetName(updateDto.Name);

        await _patientRepository.UpdateAsync(patientToUpdate);

        return new BaseResponse
        {
            StatusCode = HttpStatusCodes.NoContent,
            Message = null,
            Data = null
        };
    }

    public async Task<BaseResponse> HandleDelete(Guid id)
    {
        var patientToDelete = await _patientRepository.GetByIdAsync(id);
        if (patientToDelete == null)
            return new BaseResponse
            {
                StatusCode = HttpStatusCodes.NotFound,
                Message = "Patient not found.",
                Data = null
            };

        await _patientRepository.DeleteAsync(patientToDelete);

        return new BaseResponse
        {
            StatusCode = HttpStatusCodes.NotFound,
            Message = null,
            Data = null
        };
    }
}