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

namespace HealthcareManager.API.Controllers;

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

    public DoctorsController(IDoctorRepository database)
    {
        _database = database ?? throw new ArgumentNullException(nameof(database));
    }

    [HttpGet]
    public async Task<ActionResult<Doctor>> GetAllDoctors()
    {
        var handler = new DoctorProfileHandler(_database);
        var response = await handler.HandleGetAll();
        if (!response.Success)
        {
            return BadRequest(response);
        }

        return Ok(response.Data);
    }

    [HttpGet("{id}")]
    public async Task<ActionResult<Doctor>> GetDoctor(Guid id)
    {
        var handler = new DoctorProfileHandler(_database);
        var response = await handler.HandleGet(id);
        if (!response.Success)
        {
            return BadRequest(response);
        }

        return Ok(response.Data);
    }

    [HttpPost("login")]
    public async Task<ActionResult<Doctor>> Login(DoctorLoginDTO doctor)
    {
        var handler = new DoctorLoginHandler(_database);
        var response = await handler.Handle(doctor).ConfigureAwait(false);

        if(!response.Success) 
        {
            return Unauthorized(response);
        }

        return Ok(response);
    }

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

        if (!response.Success)
        {
            return Unauthorized(response);
        }

        return Ok(response);
    }

    [HttpPost("resetPassword")]
    public async Task<IActionResult> ResetPassword(DoctorLoginDTO resetDoctorDto)
    {
        var handler = new DoctorResetPasswordHandler(_database);
        var response = await handler.Handle(resetDoctorDto).ConfigureAwait(false);
        
        if (response.Success)
        {
            return Ok(response);
        }

        return BadRequest(response);
    }

    [HttpPut("{id}/profile")]
    public async Task<IActionResult> UpdateDoctorProfile(Guid id, [FromBody]DoctorProfileDTO doctorDto)
    {
        var handler = new DoctorProfileHandler(_database);
        var response = await handler.HandleUpdate(id, doctorDto).ConfigureAwait(false);

        if (response.Success)
        {
            return Ok(response);
        }

        return BadRequest(response);
    }

    [HttpDelete("{id}/profile")]
    public async Task<IActionResult> DeleteDoctorProfile(Guid id)
    {
        var handler = new DoctorProfileHandler(_database);
        var response = await handler.HandleDelete(id).ConfigureAwait(false);

        if (response.Success)
        {
            return NoContent();
        }

        return BadRequest(response);
    }
}
