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

namespace HealthcareManager.API.Controllers;

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

    public PacientsController(IPacientRepository database)
    {
        _database = database ?? throw new ArgumentNullException(nameof(database));
    }

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

        return Ok(response.Data);
    }

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

        return Ok(response.Data);
    }

    [HttpPost("login")]
    public async Task<ActionResult<Pacient>> Login(PacientLoginDTO pacient)
    {
        var handler = new PacientLoginHandler(_database);
        var response = await handler.Handle(pacient).ConfigureAwait(false);

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

        return Ok(response);
    }

    [HttpPost("register")]
    public async Task<ActionResult<Pacient>> Register(PacientRegistrationDto pacient)
    {
        var handler = new PacientRegistrationHandler(_database);
        var response = await handler.Handle(pacient).ConfigureAwait(false);

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

        return Ok(response);
    }

    [HttpPost("resetPassword")]
    public async Task<IActionResult> ResetPassword(PacientLoginDTO resetPacientDto)
    {
        var handler = new PacientResetPasswordHandler(_database);
        var response = await handler.Handle(resetPacientDto).ConfigureAwait(false);

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

        return BadRequest(response);
    }

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

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

        return BadRequest(response);
    }

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

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

        return BadRequest(response);
    }
}
