using Application.Endpoints;
using Application.Endpoints.MedicalHistories;
using Application.Services.Database;
using Microsoft.AspNetCore.Mvc;

namespace API.Controllers;

[ApiController]
[Route("api/[controller]")]
public class MedicalHistoryController : ControllerBase
{
    private readonly IMedicalHistoryRepository _medicalHistoryRepository;
    private readonly IMongoDbService _mongoDbService;
    private readonly IPatientRepository _patientRepository;

    public MedicalHistoryController(IMedicalHistoryRepository medicalHistoryRepository,
        IPatientRepository patientRepository, IMongoDbService mongoDbService)
    {
        _medicalHistoryRepository = medicalHistoryRepository;
        _patientRepository = patientRepository;
        _mongoDbService = mongoDbService;
    }

    [HttpGet("{id}")]
    public async Task<ActionResult<BaseResponse>> GetAsync(Guid id)
    {
        var handler = new MedicalHistoryHandler(_medicalHistoryRepository, _patientRepository, _mongoDbService);
        var response = await handler.HandleGet(id);
        return StatusCode(response.StatusCode, response);
    }

    [HttpGet]
    public async Task<ActionResult<BaseResponse>> GetAllDoctors()
    {
        var handler = new MedicalHistoryHandler(_medicalHistoryRepository, _patientRepository, _mongoDbService);
        var response = await handler.HandleGetAll();
        return StatusCode(response.StatusCode, response);
    }

    [HttpPost]
    public async Task<ActionResult<BaseResponse>> PostAsync(MedicalHistoryCreateDto medicalHistoryCreateDto)
    {
        var handler = new MedicalHistoryHandler(_medicalHistoryRepository, _patientRepository, _mongoDbService);
        var response = await handler.HandleCreate(medicalHistoryCreateDto);
        return StatusCode(response.StatusCode, response);
    }

    [HttpPut]
    public async Task<ActionResult<BaseResponse>> UpdateAsync(MedicalHistoryUpdateDto medicalHistoryUpdateDto)
    {
        var handler = new MedicalHistoryHandler(_medicalHistoryRepository, _patientRepository, _mongoDbService);
        var response = await handler.HandleUpdate(medicalHistoryUpdateDto).ConfigureAwait(false);
        return StatusCode(response.StatusCode, response);
    }

    [HttpDelete("{id}")]
    public async Task<ActionResult<BaseResponse>> DeleteAsync(Guid id)
    {
        var handler = new MedicalHistoryHandler(_medicalHistoryRepository, _patientRepository, _mongoDbService);
        var response = await handler.HandleDelete(id);
        return StatusCode(response.StatusCode, response);
    }

    /*
    [HttpPut("grant_access")]
    public async Task<ActionResult<BaseResponse>> GrantAccessToMedicalHistory(GrantAccessMedicalHistoryDto)
    {
        return NotFound();
    }
    */
}