using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CoviDok.Api;
using CoviDok.Api.Objects;
using CoviDok.Api.Request;
using CoviDok.BLL.Sessions;
using CoviDok.BLL.User.Managers;
using CoviDok.Data.MySQL;
using CoviDok.Data.SessionProviders;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace CoviDok.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class DoctorController : ControllerBase
    {
        private readonly SessionHandler Handler = new SessionHandler();

        private readonly DoctorManager doctorHandler = new DoctorManager();
        // GET /api/Doc
        [HttpGet]
        public async Task<ActionResult<ICollection<PublicDoctor>>> GetDoctors()
        {
            return await doctorHandler.GetDoctors();
        }

        [HttpGet("byFirstName/{firstName}")]
        public async Task<ActionResult<ICollection<PublicDoctor>>> GetDoctorsByFirstName(string firstName)
        {
            return await doctorHandler.GetDoctors(firstName: firstName);
        }
        [HttpGet("byLastName/{lastName}")]
        public async Task<ActionResult<ICollection<PublicDoctor>>> GetDoctorsByLastName(string lastName)
        {
            return await doctorHandler.GetDoctors(lastName: lastName);
        }

        [HttpGet("byName/{firstName}/{lastName}")]
        public async Task<ActionResult<ICollection<PublicDoctor>>> GetDoctorByName(string firstName, string lastName)
        {
            return await doctorHandler.GetDoctors(lastName: lastName, firstName: firstName);
        }

        [HttpGet("{id}")]
        public async Task<ActionResult<PublicDoctor>> GetDoctor(int id)
        {
            try
            {
                return await doctorHandler.GetDoctor(id);
            }
            catch (KeyNotFoundException)
            {
                return NotFound();
            }
        }

        [HttpPut("{id}")]
        public async Task<IActionResult> PutDoctor(int id, PublicDoctor doctor) {
            Session s = await Handler.GetSession(doctor.SessionId);
            if (s == null) return Unauthorized();
            try
            {
                await doctorHandler.UpdateDoctor(s, id, doctor);
                return NoContent();
            }
            catch (UnauthorizedAccessException)
            {
                return Unauthorized();
            }
            catch (KeyNotFoundException)
            {
                return NotFound();
            }
            catch (FormatException)
            {
                return BadRequest();
            }
        }

        // GET /api/Doc/{id}/assistants
        [HttpGet("{id}/assistants")]
        public async Task<ActionResult<ICollection<PublicAssistant>>> GetAssistantsOfDoctor(int id)
        {
            try
            {
                return await doctorHandler.GetAssistants(id);
            }
            catch (KeyNotFoundException)
            {
                return NotFound();
            }
        }

        // GET /api/Doc/{id}/children
        [HttpPost("{id}/children")]
        public async Task<ActionResult<ICollection<PublicChild>>> GetChildrenOfDoctor(int id, string SessionId)
        {
            Session s = await Handler.GetSession(SessionId);
            if (s == null) return Unauthorized();
            try
            {
                return await doctorHandler.GetChildren(id);
            }
            catch (KeyNotFoundException)
            {
                return NotFound();
            }
            catch (UnauthorizedAccessException) {
                return Unauthorized();
            }
        }
    }
}