using Application.Endpoints;
using Application.Endpoints.Doctors.Login;
using Application.Endpoints.Doctors.Profile;
using Application.Endpoints.Doctors.Registration;
using Application.Endpoints.Doctors.ResetPassword;
using Application.Services.Database;
using Application.Services.HashingAlgorithms;
using Microsoft.AspNetCore.Mvc;

namespace API.Controllers;

[ApiController]
[Route("api/[controller]")]
public class DoctorsController : ControllerBase
{
    private readonly IDoctorRepository _database;
    private readonly IHashingAlgorithms _hashingAlgorithms;

    public DoctorsController(IDoctorRepository database, IHashingAlgorithms hashingAlgorithms)
    {
        _database = database;
        _hashingAlgorithms = hashingAlgorithms;
    }

    [HttpPost("register")]
    public async Task<ActionResult<BaseResponse>> Register(DoctorRegistrationDto doctor)
    {
        var handler = new DoctorRegistrationHandler(_database, _hashingAlgorithms);
        var response = await handler.Handle(doctor).ConfigureAwait(false);
        return StatusCode(response.StatusCode, response);
    }

    [HttpPost("login")]
    public async Task<ActionResult<BaseResponse>> Login(DoctorLoginDto doctor)
    {
        var handler = new DoctorLoginHandler(_database, _hashingAlgorithms);
        var response = await handler.Handle(doctor).ConfigureAwait(false);
        return StatusCode(response.StatusCode, response);
    }

    [HttpPost("reset_password")]
    public async Task<ActionResult<BaseResponse>> ResetPassword(DoctorResetPasswordDto resetDoctorDto)
    {
        var handler = new DoctorResetPasswordHandler(_database, _hashingAlgorithms);
        var response = await handler.Handle(resetDoctorDto).ConfigureAwait(false);
        return StatusCode(response.StatusCode, response);
    }

    [HttpGet]
    public async Task<ActionResult<BaseResponse>> GetAllDoctors()
    {
        var handler = new DoctorProfileHandler(_database, _hashingAlgorithms);
        var response = await handler.HandleGetAll();
        return StatusCode(response.StatusCode, response);
    }

    [HttpGet("{id}")]
    public async Task<ActionResult<BaseResponse>> GetDoctor(Guid id)
    {
        var handler = new DoctorProfileHandler(_database, _hashingAlgorithms);
        var response = await handler.HandleGet(id);
        return StatusCode(response.StatusCode, response);
    }

    [HttpPut]
    public async Task<ActionResult<BaseResponse>> UpdateDoctorProfile(DoctorProfileUpdateDto doctorUpdateDto)
    {
        var handler = new DoctorProfileHandler(_database, _hashingAlgorithms);
        var response = await handler.HandleUpdate(doctorUpdateDto).ConfigureAwait(false);
        return StatusCode(response.StatusCode, response);
    }

    [HttpDelete("{id}")]
    public async Task<ActionResult<BaseResponse>> DeleteDoctorProfile(Guid id)
    {
        var handler = new DoctorProfileHandler(_database, _hashingAlgorithms);
        var response = await handler.HandleDelete(id).ConfigureAwait(false);
        return StatusCode(response.StatusCode, response);
    }
}