using Shared.DTOs;
using Back.Data.Contracts;
using Back.Data.Models;
using Microsoft.EntityFrameworkCore;

namespace Back.Services
{
    public class ServValidatinMsg
    {
        private readonly IAsyncRepository<VerificationCode> _verificationCodeRepo;
        private readonly IAsyncRepository<Ticket> _ticket;
        private readonly IAsyncRepository<User> _UserRepo;
        private readonly IAsyncRepository<Company> _CompanyRepo;
        private readonly servSendMsg _servSendMsg;
        public ServValidatinMsg(IAsyncRepository<VerificationCode> verificationCodeRepo
            , IAsyncRepository<Ticket> ticket, IAsyncRepository<User> UserRepo
            , IAsyncRepository<Company> CompanyRepo, servSendMsg servSendMsg)
        {
            _verificationCodeRepo = verificationCodeRepo;
            _ticket = ticket;
            _UserRepo = UserRepo;
            _CompanyRepo = CompanyRepo;
            _servSendMsg = servSendMsg;
        }
        public async Task<VerificationCode> GetCodeByPrm(string Prm)
        {
            return await _verificationCodeRepo.Get(w => w.prm == Prm).FirstOrDefaultAsync();

        }
        public async Task<VerificationCode> GetVerificationCode(int ID)
        {
            return await _verificationCodeRepo.Get(w => w.Code == ID).FirstOrDefaultAsync();
        }
        public async Task<VerificationCode> GetVerificationCodeByID(int ID)
        {
            return await _verificationCodeRepo.Get(w => w.ID == ID).FirstOrDefaultAsync();
        }
        public async Task<bool> Delete(VerificationCode model)
        {
            return await _verificationCodeRepo.DeleteAsync(model);
        }
        public async Task<bool> SubmittedTicket(VerificationCode code)
        {
            var ticket = await _ticket.Get(w => w.ID == Convert.ToInt32(code.prm)).FirstOrDefaultAsync();
            if (ticket != null && ticket.Status== StatusTicket.unknownPerson)
            {
                ticket.Status = StatusTicket.Awaitingreview;
               return await _ticket.UpdateAsync(ticket);
            }
            return false;
        }
        public async Task<bool> SubmittedCompanyRegistration(VerificationCode code)
        {
            var user = await _UserRepo.Get(w => w.ID == Convert.ToInt32(code.val) && !w.IsActive).FirstOrDefaultAsync();
            var company = await _CompanyRepo.Get(w => w.ID == Convert.ToInt32(code.prm) && !w.IsActive).FirstOrDefaultAsync();
            if (user != null && company != null)
            {
                user.IsActive = true;
                if (await _UserRepo.UpdateAsync(user) != null)
                {
                    company.IsActive = true;
                    if (await _CompanyRepo.UpdateAsync(company))
                    {
                        _servSendMsg.SuccessfulRegistration(user.Mobile, user.Mobile,user.Mobile);
                        return true;
                    }

                }
            }
            return false;
        }
        public async Task<VerificationCode> GenerateCode(VerificationCode code)
        {
            code.Code = Random.Shared.Next(1000, 9000);
            while (await GetVerificationCode(code.Code) != null)
                code.Code = Random.Shared.Next(1000, 9000);


          var indb= await _verificationCodeRepo.AddAsync(code);
            code.ID = indb.ID;
            return code;
        }
    }
}
