using Microsoft.EntityFrameworkCore;
using Npgsql;

namespace Register_office.ViewModel.LoginVM
{
	internal class LoginViewModel
	{
		private readonly GeneralViewModel _general;

		internal GeneralViewModel General => _general;

		public LoginViewModel()
		{
			_general = GeneralViewModel.Instance;
		}

		/// <summary>
		/// Проверяет логин и пароль на наличие и соответсствие в БД
		/// </summary>
		/// <param name="login">Логин пользователя</param>
		/// <param name="password">Пароль пользователя</param>
		/// <exception cref="NpgsqlException"></exception>
		internal int CheckEnter(string login, string password)
		{
			login = login.Trim();
			password = password.Trim();
			return _general.Context.Database
				.SqlQueryRaw<int>("SELECT * FROM check_enter_in_person_account({0}, {1}) as Value",
				login, password)
				.AsEnumerable()
				.First();
		}

		internal int GetDoctorId(int personId)
		{
			int id = _general.Context.Doctors
				.Where(x => x.DoctorId == personId)
				.Select(x => x.DoctorId)
				.FirstOrDefault();
			return id;
		}
	}
}
