using Lada.Framework.Data.Domains;
using Lada.Framework.Data.Domains.Cms;
using Lada.Framework.Data.Domains.Hr;
using Lada.Framework.Data.Repositories;
using Lada.Framework.DTO;
using Lada.Framework.Infrastructures.Helpers;
using Lada.Framework.Infrastructures.Services;
using ladaexpress.vn.Infrastructures.DTO;
using ladaexpress.vn.Infrastructures.DTO.Models.Hr;
using ladaexpress.vn.Infrastructures.DTO.Results.Hr;
using Microsoft.EntityFrameworkCore;
using System;
using System.Threading.Tasks;
namespace ladaexpress.vn.Infrastructures.Services
{
    public interface ICandidateService
    {
        Task<ApplyResult> Apply(ApplyModel mode);
    }

    public class CandidateService : ICandidateService
    {
        private readonly IRepository<Candidate> _candidateRepository;
        private readonly ICvUploadService _cvUploadService;
        public CandidateService(IRepository<Candidate> candidateRepository, ICvUploadService cvUploadService)
        {
            _candidateRepository = candidateRepository;
            _cvUploadService = cvUploadService;
        }

        public async Task<ApplyResult> Apply(ApplyModel model)
        {
            var result = new ApplyResult();

            try
            {
                // Kiểm tra ứng tuyển trùng lặp (cùng JobId và PhoneNumber)
                var existingApplication = await _candidateRepository.Query()
                    .Include(c => c.Job)
                    .FirstOrDefaultAsync(c => c.JobId == model.JobPostId && c.PhoneNumber == model.PhoneNumber);

                if (existingApplication != null)
                {
                    result.Result = Result.Failed;
                    var jobTitle = existingApplication.Job?.Title ?? "vị trí này";
                    var statusText = existingApplication.Status.GetDescription();
                    result.Message = $"Bạn đã ứng tuyển {jobTitle} rồi. Vui lòng chờ phản hồi từ công ty.";
                    return result;
                }

                var candidate = model.ToCandidate();
                candidate.CreatedBy = 0;
                candidate.CreatedByUserName = "";
                candidate.UpdatedBy = 0;
                candidate.UpdatedByUserName = "";
                candidate.Note = "";
                candidate.StatusContent = "";
                candidate.Status = CandidateStatus.New;
                candidate.UpdateCommonInt();
                candidate.CvFileName = string.Empty;
                candidate.CvFilePath = string.Empty;
                candidate.CvFileName = string.Empty;
                candidate.CvFileType = string.Empty;
                candidate.CvFileUrl = string.Empty;
                // Xử lý upload CV nếu có
                if (model.CvFile != null)
                {
                    var cvUploadResult = await _cvUploadService.UploadCvAsync(model.CvFile);
                    if (cvUploadResult.Result == Result.Success)
                    {
                        candidate.CvFileName = cvUploadResult.FileName;
                        candidate.CvFilePath = cvUploadResult.FilePath;
                        candidate.CvFileSize = cvUploadResult.FileSize;
                        candidate.CvFileType = cvUploadResult.FileType;
                        candidate.CvFileUrl = cvUploadResult.FileUrl;
                        candidate.CvUploadedTime = cvUploadResult.UploadedTime;
                    }
                    else
                    {
                        result.Result = Result.Failed;
                        result.Message = $"Lỗi upload CV: {cvUploadResult.Message}";
                        return result;
                    }
                }

                await _candidateRepository.InsertAsync(candidate);
                result.Id = candidate.Id;

            }
            catch (Exception e)
            {
                result.Result = Result.SystemError;
                result.Message = e.ToString();
                result.Id = 0;
            }
            return result;
        }
    }
}
