using Microsoft.AspNetCore.Mvc;
using UniversityContracts.BindingModels;
using UniversityContracts.BusinessLogicContracts;
using UniversityContracts.SearchModels;
using UniversityContracts.ViewModels;

namespace UniversityRestAPI.Controllers
{
	[Route("api/[controller]/[action]")]
	[ApiController]
	public class StudentController : Controller
	{
		private readonly IStudentLogic _studentLogic;

		public StudentController(IStudentLogic studentLogic)
		{
			_studentLogic = studentLogic;
		}

		[HttpGet]
		public StudentViewModel? Get(int id)
		{
			try
			{
				return _studentLogic.ReadElement(new StudentSearchModel { Id = id });
			}
			catch (Exception ex)
			{				
				throw;
			}
		}

        [HttpGet]
        public List<StudentViewModel>? GetAll()
        {
            try
            {
                return _studentLogic.ReadList(null);
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        [HttpGet]
		public List<StudentViewModel>? GetAllByUser(int userId)
		{
			try
			{
				return _studentLogic.ReadList(new StudentSearchModel { UserId = userId });
			}
			catch (Exception ex)
			{				
				throw;
			}
		}

        [HttpGet]
        public List<StudentViewModel>? GetAllByUserAndEducationStatus(int userId, int educationstatus)
        {
            try
            {
                return _studentLogic.ReadList(new StudentSearchModel { UserId = userId, EducationStatusId = educationstatus });
            }
            catch (Exception ex)
            {
                throw;
            }
        }
		[HttpGet]
        public List<StudentViewModel>? GetAllByUserAndStream(int userId, int streamId)
        {
            try
            {

                return _studentLogic.ReadList(new StudentSearchModel { UserId = userId});
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        [HttpGet]
		public List<StudentViewModel>? GetMany(int userId, int page)
		{
			try
			{
				return _studentLogic.ReadList(new StudentSearchModel { UserId = userId, PageNumber = page, PageSize = 10 });
			}
			catch (Exception ex)
			{				
				throw;
			}
		}

		[HttpGet]
		public int GetNumberOfPages(int userId)
		{
			try
			{
				return _studentLogic.GetNumberOfPages(userId);
			}
			catch (Exception ex)
			{
				throw;
			}
		}

		[HttpPost]
		public void Create(StudentBindingModel model)
		{
			try
			{
				_studentLogic.Create(model);
			}
			catch (Exception ex)
			{
				throw;
			}
		}

		[HttpPost]
		public void Update(StudentBindingModel model)
		{
			try
			{
				_studentLogic.Update(model);
			}
			catch (Exception ex)
			{
				throw;
			}
		}

		[HttpPost]
		public void Delete(StudentBindingModel model)
		{
			try
			{
				_studentLogic.Delete(new() { Id = model.Id });
			}
			catch (Exception ex)
			{
				throw;
			}
		}
		[HttpGet]
		public List<StudentViewModel> GetStudentsFromStream(int streamId ) {
            try
            {

                return _studentLogic.GetStudentsFromStream(streamId);
            }
            catch (Exception ex)
            {
                throw;
            }
        }
	}
}