using LSFE.Infrastructure.Dto.Maintenance;
using LSFE.Infrastructure.Dto.Planning;
using LSFE.Infrastructure.Entities;
using LSFE.Infrastructure.Entities.Doctor;
using LSFE.Infrastructure.Entities.Maintenance;
using LSFE.Infrastructure.Entities.Planning;
using LSFE.Infrastructure.Entities.TimeKeeping;
using LSFE.MobApp.Entities.Location;
using System.Threading.Tasks;

namespace LSFE.MobApp.Mapper
{
    public static class ApiEntityMapper
    {
        #region Get Mapping
        public static Entities.Doctor.Doctor MapDoctorFromApi(Entities.Doctor.Doctor apiEntity)
        {
            return new Entities.Doctor.Doctor
            {
                Id=Guid.NewGuid(),
                DoctorId = apiEntity.DoctorId,
                FirstName = apiEntity.FirstName,
                LastName = apiEntity.LastName,
                MiddleInitial = apiEntity.MiddleInitial,
                MaxVisit = apiEntity.MaxVisit,
                SpecializationName = apiEntity.SpecializationName,
                PhoneNo = apiEntity.PhoneNo,
                BirthDate = apiEntity.BirthDate,
                EmailAddress = apiEntity.EmailAddress,
                LicenseNo = apiEntity.LicenseNo,
                InstitutionId = apiEntity.InstitutionId,
                InstitutionName = apiEntity.InstitutionName,
                Address = apiEntity.Address,
                Status = apiEntity.Status,
                IsTerritorial = apiEntity.IsTerritorial,
                DistrictName = apiEntity.DistrictName,
                MRRawPlanId = apiEntity.MRRawPlanId,
                IsCatchUpCall = apiEntity.IsCatchUpCall,
                CreatedDate =DateTime.Now
            };
        }
        public static Institution MapInstutionFromApi(Institutions apiEntity)
        {
            return new Institution
            {
                Id = Guid.NewGuid(), // Generate new GUID for local unique ID
                InstitutionId = apiEntity.InstitutionId, // Store the server ID
                InstitutionName = apiEntity.InstitutionName,
                TerritoryId = apiEntity.TerritoryId,
                IsActive = apiEntity.IsActive,
                LastSyncedAt = DateTime.UtcNow,
                NeedsSync = false,
                CreatedDate = DateTime.UtcNow
            };
        }
        public static Entities.Account.Attendance MapAttendanceFromApi(Attendance apiEntity)
        {
            return new Entities.Account.Attendance
            {
                AppAttendanceId = Guid.Parse(apiEntity.AppAttendanceId),
                TimeIn = apiEntity.TimeIn,
                TimeOut = apiEntity.TimeOut,
                UserId = apiEntity.UserId,
                TimeInLocation = apiEntity.TimeInLocation,
                TimeOutLocation = apiEntity.TimeOutLocation,
                SignatureFileNameIn = apiEntity.SignatureFileNameIn,
                SignatureFileNameOut= apiEntity.SignatureFileNameOut
            };
        }
        
        public static Entities.Planning.ProductTransaction MapProductTransFromApi(ProductTransactionDto apiEntity)
        {
            return new Entities.Planning.ProductTransaction
            {
                Id = Guid.NewGuid(),
                ProductTransId=apiEntity.ProductTransId,
                ProductId = apiEntity.ProductId,
                DoctorId = apiEntity.DoctorId,
                AppointmentDate = apiEntity.AppointmentDate.HasValue
                                ? apiEntity.AppointmentDate.Value.ToDateTime(TimeOnly.MinValue)
                                : default, 
                ProductName = apiEntity.ProductName,
                ProductLink = apiEntity.ProductLink,
                UserId = apiEntity.UserId,
                IsActive = apiEntity.IsActive
            };
        }
        public static Entities.Planning.Product MapProductFromApi(Products apiEntity)
        {
            return new Entities.Planning.Product
            {
                Id = Guid.NewGuid(),
                ProductId = apiEntity.ProductId,
                ProductName = apiEntity.ProductName,
                ProductLink = apiEntity.ProductLink,
                UserId = apiEntity.UserId,
                IsActive = apiEntity.IsActive
            };
        }
        public static Entities.Planning.InventoryTransaction MapInventoryTransFromApi(InventoryTransaction apiEntity)
        {
            return new Entities.Planning.InventoryTransaction
            {
                Id = Guid.NewGuid(),
                InventoryTransId = apiEntity.InventoryTransId,
                InventoryId = apiEntity.InventoryId,
                Description = apiEntity.Description,
                DoctorId= apiEntity.DoctorId,
                MRRawPlanId= apiEntity.MRRawPlanId,
                AppointmentDate = apiEntity.AppointmentDate.HasValue
                                ? apiEntity.AppointmentDate.Value.ToDateTime(TimeOnly.MinValue)
                                : default, 
                QtyIn = apiEntity.QtyIn,
                UserId = apiEntity.UserId,
                IconEmojiId = apiEntity.IconEmojiId,
                CreatedBy = apiEntity.CreatedBy,
                UpdatedDate= apiEntity.UpdatedDate,
                UpdatedBy= apiEntity.UpdatedBy,
                CreatedDate= apiEntity.CreatedDate,
                IsActive = apiEntity.IsActive
            };
        }
        public static Entities.Planning.MRRawPlan MapMRRawPlanFromApi(MRRawPlansDto apiEntity)
        {
            return new Entities.Planning.MRRawPlan
            {
                Id = Guid.NewGuid(),
                MRRawPlanId = apiEntity.MRRawPlanId,
                MRPlanDetailId = apiEntity.MRPlanDetailId,
                DoctorId = apiEntity.DoctorId,
                VisitCount = apiEntity.VisitCount,
                CallDate = apiEntity.CallDate.HasValue
                                ? apiEntity.CallDate.Value.ToDateTime(TimeOnly.MinValue)
                                : default,
                ActualCallDate = apiEntity.ActualCallDate.HasValue
                                ? apiEntity.ActualCallDate.Value.ToDateTime(TimeOnly.MinValue)
                                : default,
                StartTime = apiEntity.StartTime,
                EndTime = apiEntity.EndTime,
                Longitude = apiEntity.Longitude,
                Latitude = apiEntity.Latitude,
                Location = apiEntity.Location,
                IsSignature = apiEntity.IsSignature,
                SignatureFileName = apiEntity.SignatureFileName,
                IsMissed = apiEntity.IsMissed,
                MissedReason = apiEntity.MissedReason,
                IsDoctorProxy = apiEntity.IsDoctorProxy,
                IsReschedule = apiEntity.IsReschedule,
                RescheduleReason = apiEntity.RescheduleReason,
                CallType = apiEntity.CallType,
                IsJoinCall = apiEntity.IsJoinCall,
                IsCatchUpCall = apiEntity.IsCatchUpCall,
                CycleId = apiEntity.CycleId,
                IsApproved = apiEntity.IsApproved,
                IsRecovered = apiEntity.IsRecovered,
                SLP = apiEntity.SLP,
                Status= apiEntity.Status,
                ApprovedBy = apiEntity.ApprovedBy,
                ApprovedDate = apiEntity.ApprovedDate
            };
        }
        public static Entities.Account.User MapUserFromApi(AppsUsers apiEntity)
        {
            return new Entities.Account.User
            {
                Id = Guid.NewGuid(),             
                UserId = apiEntity.Id,
                UserName = apiEntity.UserName,
                FullName = apiEntity.FullName,
                Password = apiEntity.PasswordHash,
                Email = apiEntity.Email ?? "N/A",
                PhoneNumber = apiEntity.PhoneNumber ?? "N/A",
                EmployeeId = apiEntity.EmployeeId,
                Company = apiEntity.Company ?? "N/A",
                LockoutEnabled = apiEntity.LockoutEnabled
            };
        }
        #endregion

        #region Update Mapping
        public static void UpdateUserFromApi(Entities.Account.User existing,
        AppsUsers apiEntity)
        {
            existing.UserId = apiEntity.Id;
            existing.UserName = apiEntity.UserName;
            existing.FullName = apiEntity.FullName;
            existing.Password = apiEntity.PasswordHash;
            existing.Email = apiEntity.Email ?? "N/A";
            existing.PhoneNumber = apiEntity.PhoneNumber ?? "N/A";
            existing.EmployeeId = apiEntity.EmployeeId;
            existing.Company = apiEntity.Company ?? "N/A";
            existing.LockoutEnabled = apiEntity.LockoutEnabled;
        }
        public static void UpdateMRRawPlanFromApi(Entities.Planning.MRRawPlan existing,
       MRRawPlansDto apiEntity)
        {
            existing.MRRawPlanId = apiEntity.MRRawPlanId;
            existing.MRPlanDetailId = apiEntity.MRPlanDetailId;
            existing.DoctorId = apiEntity.DoctorId;
            existing.VisitCount = apiEntity.VisitCount;
            existing.CallDate = apiEntity.CallDate.HasValue
                            ? apiEntity.CallDate.Value.ToDateTime(TimeOnly.MinValue)
                            : default;
            existing.ActualCallDate = apiEntity.ActualCallDate.HasValue
                            ? apiEntity.ActualCallDate.Value.ToDateTime(TimeOnly.MinValue)
                            : default;
            existing.StartTime = apiEntity.StartTime;
            existing.EndTime = apiEntity.EndTime;
            existing.Longitude = apiEntity.Longitude;
            existing.Latitude = apiEntity.Latitude;
            existing.Location = apiEntity.Location;
            existing.IsSignature = apiEntity.IsSignature;
            existing.SignatureFileName = apiEntity.SignatureFileName;
            existing.IsMissed = apiEntity.IsMissed;
            existing.MissedReason = apiEntity.MissedReason;
            existing.IsDoctorProxy = apiEntity.IsDoctorProxy;
            existing.IsReschedule = apiEntity.IsReschedule;
            existing.RescheduleReason = apiEntity.RescheduleReason;
            existing.CallType = apiEntity.CallType;
            existing.IsJoinCall = apiEntity.IsJoinCall;
            existing.CycleId = apiEntity.CycleId;
            existing.IsApproved = apiEntity.IsApproved;
            existing.IsRecovered = apiEntity.IsRecovered;
            existing.SLP = apiEntity.SLP;
            existing.ApprovedBy = apiEntity.ApprovedBy;
            existing.ApprovedDate = apiEntity.ApprovedDate;
            existing.IsCatchUpCall = apiEntity.IsCatchUpCall;
            existing.Status = apiEntity.Status;
            existing.ForAddressTranslation = false;   
        }
        public static void UpdateInventoryTransFromApi(Entities.Planning.InventoryTransaction existing,
        InventoryTransaction apiEntity)
        {
            existing.InventoryTransId = apiEntity.InventoryTransId;
            existing.InventoryId = apiEntity.InventoryId;
            existing.Description = apiEntity.Description;
            existing.DoctorId = apiEntity.DoctorId;
            existing.MRRawPlanId = apiEntity.MRRawPlanId;

            // Convert DateOnly to DateTime
            existing.AppointmentDate = apiEntity.AppointmentDate.HasValue
            ? apiEntity.AppointmentDate.Value.ToDateTime(TimeOnly.MinValue)
            : default; 

            existing.QtyIn = apiEntity.QtyIn;
            existing.UserId = apiEntity.UserId;
            existing.CreatedBy = apiEntity.CreatedBy;
            existing.UpdatedDate = apiEntity.UpdatedDate;
            existing.UpdatedBy = apiEntity.UpdatedBy;
            existing.CreatedDate = apiEntity.CreatedDate;
            existing.IconEmojiId = apiEntity.IconEmojiId;
            existing.IsActive = apiEntity.IsActive;
        }      
        public static void UpdateProductTransFromApi(Entities.Planning.ProductTransaction existing,
           ProductTransactionDto apiEntity)
        {
            existing.ProductTransId = apiEntity.ProductTransId;
            existing.ProductId = apiEntity.ProductId;
            existing.ProductName = apiEntity.ProductName;
            existing.AppointmentDate =apiEntity.AppointmentDate.HasValue
                                ? apiEntity.AppointmentDate.Value.ToDateTime(TimeOnly.MinValue)
                                : default;
            existing.DoctorId = apiEntity.DoctorId;
            existing.ProductLink = apiEntity.ProductLink;
            existing.UserId = apiEntity.UserId;
            existing.IsActive = apiEntity.IsActive;
        }
        public static void UpdateProductFromApi(Entities.Planning.Product existing,
        Products apiEntity)
        {
            existing.ProductId = apiEntity.ProductId;
            existing.ProductName = apiEntity.ProductName;
            existing.ProductLink = apiEntity.ProductLink;
            existing.UserId = apiEntity.UserId;
            existing.IsActive = apiEntity.IsActive;
        }
        public static void UpdateDoctorFromApi(Entities.Doctor.Doctor existing,
            Entities.Doctor.Doctor apiEntity)
        {
            existing.DoctorId = apiEntity.DoctorId;
            existing.FirstName = apiEntity.FirstName;
            existing.LastName = apiEntity.LastName;
            existing.MiddleInitial = apiEntity.MiddleInitial;
            existing.MaxVisit = apiEntity.MaxVisit;
            existing.SpecializationName = apiEntity.SpecializationName;
            existing.PhoneNo = apiEntity.PhoneNo;
            existing.BirthDate = apiEntity.BirthDate;
            existing.EmailAddress = apiEntity.EmailAddress;
            existing.LicenseNo = apiEntity.LicenseNo;
            existing.InstitutionId = apiEntity.InstitutionId;
            existing.InstitutionName = apiEntity.InstitutionName;
            existing.Address = apiEntity.Address;
            existing.Status = apiEntity.Status;
            existing.IsTerritorial = apiEntity.IsTerritorial;
            existing.DistrictName = apiEntity.DistrictName;
            existing.MRRawPlanId = apiEntity.MRRawPlanId;
            existing.IsCatchUpCall = apiEntity.IsCatchUpCall;
            existing.CreatedDate = DateTime.Now;
        }
        public static void UpdateInstutionFromApi(Institution existing,
            Institutions apiEntity)
        {
            existing.InstitutionId = apiEntity.InstitutionId;
            existing.InstitutionName = apiEntity.InstitutionName;
            existing.TerritoryId = apiEntity.TerritoryId;
            existing.IsActive = apiEntity.IsActive;
            existing.LastSyncedAt = DateTime.UtcNow;
            existing.NeedsSync = false;
        }
        #endregion
    }
}
