using CoviDok.Api.Objects;
using CoviDok.BLL.Sessions;
using CoviDok.BLL.User.Managers;
using CoviDok.Data.MySQL;
using CoviDok.Data.Model;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace CoviDok.Data.MySQL
{
    public class AssistantManager
    {
        private readonly IAssistantHandler handler = new MySqlAssistantHandler();
        public async Task<PublicAssistant> GetAssistant(int id)
        {
            Assistant ast = await handler.GetAssistant(id);
            if (ast == null) throw new KeyNotFoundException();
            return ast.ToPublic();
        }

        public async Task UpdateAssistant(Session s, int id, PublicAssistant value)
        {
            if (id != value.ID) throw new FormatException();
            if (s.ID != id) throw new UnauthorizedAccessException();

            Assistant ast = await handler.GetAssistant(id);
            if (ast == null) throw new KeyNotFoundException();

            ast.UpdateSelf(value);
            await handler.SetAssistant(id, ast);            
        }        
    }
}