using CandidateReviewContracts.BindingModels;
using CandidateReviewContracts.BusinessLogicsContracts;
using CandidateReviewContracts.SearchModels;
using CandidateReviewContracts.ViewModels;
using CandidateReviewDataModels.Models;
using Microsoft.AspNetCore.Mvc;

namespace CandidateReviewRestApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class AssessmentController : Controller
    {
        private readonly ILogger _logger;
        private readonly IAssessmentLogic _logic;
        private readonly IAssessmentCriterionLogic _logicAC;
        public AssessmentController(IAssessmentLogic logic, ILogger<AssessmentController> logger, IAssessmentCriterionLogic logicAC)
        {
            _logger = logger;
            _logic = logic;
            _logicAC = logicAC;
        }

        [HttpGet]
        public AssessmentViewModel? Details(int id)
        {
            try
            {
                return _logic.ReadElement(new AssessmentSearchModel
                {
                    UserId = id
                });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка получения оценки");
                throw;
            }
        }

        [HttpGet]
        public List<AssessmentViewModel>? ListByResumeId(int id)
        {
            try
            {
                return _logic.ReadList(new AssessmentSearchModel
                {
                    ResumeId = id
                });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка получения оценок");
                throw;
            }
        }

        [HttpGet]
        public List<AssessmentCriterionViewModel>? AssessmentCriterions(int assessmentId)
        {
            try
            {
                return _logicAC.ReadList(new AssessmentCriterionSearchModel
                {
                    AssessmentId = assessmentId
                });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка получения оценок");
                throw;
            }
        }

        [HttpPost]
        public IActionResult Create(AssessmentBindingModel model)
        {
            try
            {
                int? id = _logic.Create(model);
                return Ok(new AssessmentBindingModel { Id = (int)id });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка создания оценки");
                throw;
            }
        }

        [HttpPost]
        public void CreateCriterion(AssessmentCriterionModel model)
        {
            try
            {
                _logicAC.Create(model);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка создания оценки");
                throw;
            }
        }

        [HttpPost]
        public void Update(AssessmentBindingModel model)
        {
            try
            {
                _logic.Update(model);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка обновления оценки");
                throw;
            }
        }

        [HttpPost]
        public void Delete(AssessmentBindingModel model)
        {
            try
            {
                _logic.Delete(model);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка удаления оценки");
                throw;
            }
        }
    }
}
