using Application.Services.Database;
using Core.Entities;

namespace Application.Endpoints.MedicalHistories;

public class MedicalHistoryHandler
{
    private readonly IMedicalHistoryRepository _medicalHistoryRepository;
    private readonly IPacientRepository _pacientRepository;

    public MedicalHistoryHandler(IMedicalHistoryRepository medicalHistoryRepository, IPacientRepository pacientRepository)
    {
        _medicalHistoryRepository = medicalHistoryRepository;
        _pacientRepository = pacientRepository;
    }
    public async Task<BaseResponse> HandleGet(Guid id)
    {
        var medicalHistory = await _medicalHistoryRepository.GetByIdAsync(id).ConfigureAwait(false);
        if (medicalHistory != null)
        {
            return new BaseResponse
            {
                Success = true,
                Message = $"Retrieved Medical History with id: {id}",
                Data = medicalHistory
            };
        }

        return new BaseResponse
        {
            Success = false,
            Message = $"Medical History with id: {id} not found",
            Data = null
        };
    }

    public async Task<BaseResponse> HandleCreate(Guid userId, byte[] description)
    {
        var validation = new MedicalHistoryCreateValidation(_pacientRepository);
        var validationResult = await validation.ValidateAsync(new MedicalHistoryDTO { UserId = userId, Description = description});

        if (!validationResult.IsValid)
        {
            var errorMessage = validationResult.Errors.Select(e => e.ErrorMessage).ToList();
            return new BaseResponse
            {
                Success = false,
                Message = string.Join(", ", errorMessage),
                Data = null
            };
        }

        var medicalHistory = new MedicalHistory
        {
            UserId = userId,
            Description = description
        };

        await _medicalHistoryRepository.AddAsync(medicalHistory);

        return new BaseResponse
        {
            Success = true,
            Message = "Medical history record registered successfully",
            Data = medicalHistory
        };
    }

    public async Task<BaseResponse> HandleUpdate(Guid id, MedicalHistoryDTO updateDto)
    {
        var validation = new MedicalHistoryUpdateValidation(_medicalHistoryRepository, _pacientRepository);
        var validationResult = await validation.ValidateAsync(new MedicalHistoryUpdateDTO { Id = id, UserId = updateDto.UserId, Description = updateDto.Description});

        if (!validationResult.IsValid)
        {
            var errorMessage = validationResult.Errors.Select(e => e.ErrorMessage).ToList();
            return new BaseResponse
            {
                Success = false,
                Message = string.Join(", ", errorMessage),
                Data = null
            };
        }

        var medicalHistoryToUpdate = await _medicalHistoryRepository.GetByIdAsync(id);

        if (medicalHistoryToUpdate == null)
        {
            return new BaseResponse
            {
                Success = false,
                Message = "Medical history not found for given Id",
                Data = null
            };
        }

        medicalHistoryToUpdate.UserId = updateDto.UserId;
        medicalHistoryToUpdate.Description = updateDto.Description;

        await _medicalHistoryRepository.UpdateAsync(medicalHistoryToUpdate);

        return new BaseResponse
        {
            Success = true,
            Message = "Pacient updated successfully",
            Data = medicalHistoryToUpdate
        };
    }
}
