using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CoviDok.Api.Objects;
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 AssistantController : ControllerBase
    {
        private readonly SessionHandler Handler = new SessionHandler();

        private readonly AssistantManager mgr = new AssistantManager();


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

        [HttpPut("{id}")]
        public async Task<IActionResult> PutAssistant(int id, PublicAssistant ast)
        {
            try
            {
                Session s = await Handler.GetSession(ast.SessionId);
                await mgr.UpdateAssistant(s, id, ast);
                return NoContent();
            }
            catch (UnauthorizedAccessException)
            {
                return Unauthorized();
            }
            catch (KeyNotFoundException)
            {
                return NotFound();
            }
        }
    }
}