using Lada.Framework.Data.Domains.Crm;
using Lada.Framework.Data.Repositories;
using Lada.Framework.DTO;
using Lada.Framework.Infrastructures.Helpers;
using ladaexpress.vn.Infrastructures.DTO;
using ladaexpress.vn.Infrastructures.DTO.Models.Service;
using ladaexpress.vn.Infrastructures.DTO.Results.Crm;

namespace ladaexpress.vn.Infrastructures.Services
{
    public interface ILeadService
    {
        /// <summary>
        /// Tạo lead.
        /// </summary>
        /// <param name="model">Thông tin lead.</param>
        /// <returns>Kết quả của thao tác.</returns>
        Task<LeadResult> Create(LeadModel model);
    }
    public class LeadService : ILeadService
    {
        private readonly IRepository<Lead> _leadRepository;

        public LeadService(IRepository<Lead> leadRepository)
        {
            _leadRepository = leadRepository;
        }


        public async Task<LeadResult> Create(LeadModel model)
        {
            var result = new LeadResult();
            var currentTime = DateTimeHelper.CurrentTime();


            var lead = model.ToEntity();
            lead.CreatedTime = currentTime;
            lead.UpdatedTime = currentTime;
            try
            {

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