using Microsoft.EntityFrameworkCore;
using BMA.EHR.Recurit.Exam.Service.Data;
using System.Data;

namespace BMA.EHR.Recurit.Exam.Service.Services
{
    public class DisableService
    {
        private readonly ApplicationDbContext _context;
        private readonly IConfiguration _configuration;
        private readonly IWebHostEnvironment _webHostEnvironment;

        public DisableService(ApplicationDbContext context,
                            IConfiguration configuration,
                            IWebHostEnvironment webHostEnvironment)
        {
            _context = context;
            _configuration = configuration;
            _webHostEnvironment = webHostEnvironment;
        }

        public int GetExamCount(string citizenId)
        {
            try
            {
                var count = _context.Candidates.AsQueryable()
                                    .Where(x => x.CitizenId == citizenId)
                                    .Count();

                return count;
            }
            catch
            {
                throw;
            }
        }

        public async Task<string> GetExamAttributeAsync(Guid period, Guid exam)
        {
            try
            {
                var payment = await _context.DisablePayments.AsQueryable()
                                        .Include(x => x.Disable)
                                        .ThenInclude(x => x.PeriodExam)
                                        .Where(x => x.Disable.Id == exam)
                                        .Where(x => x.Disable.PeriodExam.Id == period)
                                        .FirstOrDefaultAsync();

                return payment != null ? "มีคุณสมบัติ" : "ไม่มีคุณสมบัติ";
            }
            catch
            {
                throw;
            }
        }

        public bool CheckValidCertificate(DateTime certDate, int nextYear = 5)
        {
            var valid = true;
            if (DateTime.Now.Date > certDate.Date.AddYears(nextYear))
                valid = false;

            return valid;
        }

    }
}