using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using SolutionErp.Domain.Hrm;
using SolutionErp.Domain.Identity;

namespace SolutionErp.Infrastructure.Persistence.Configurations;

// EF Mig 34 — Hồ sơ Nhân sự main entity.
// 1-1 User qua UNIQUE UserId. FK Cascade Users (xoá user → wipe profile).
// EmployeeCode UNIQUE. Phone INDEX (lookup nhanh).
// IsDeleted INDEX (soft delete query filter).
//
// Province/District/Ward 6 cột nullable plain Guid? — KHÔNG declare FK
// constraint (catalog chưa scaffold trong Mig 34, defer G-H2 khi thêm catalog
// thì alter table add FK).
public class EmployeeProfileConfiguration : IEntityTypeConfiguration<EmployeeProfile>
{
    public void Configure(EntityTypeBuilder<EmployeeProfile> e)
    {
        e.ToTable("EmployeeProfiles");

        // ===== EmployeeCode + UserId UNIQUE =====
        e.Property(x => x.EmployeeCode).HasMaxLength(50).IsRequired();
        e.HasIndex(x => x.EmployeeCode).IsUnique();

        e.HasIndex(x => x.UserId).IsUnique();

        // ===== 1-1 User FK Cascade =====
        e.HasOne(x => x.User)
            .WithOne()
            .HasForeignKey<EmployeeProfile>(x => x.UserId)
            .OnDelete(DeleteBehavior.Cascade);

        // ===== Cá nhân =====
        e.Property(x => x.BirthPlace).HasMaxLength(200);
        e.Property(x => x.Hometown).HasMaxLength(200);

        e.Property(x => x.Phone).HasMaxLength(20);
        e.HasIndex(x => x.Phone);  // Non-unique (nhiều NV cùng nhà có thể chung SDT cố định)
        e.Property(x => x.PersonalEmail).HasMaxLength(200);
        e.Property(x => x.InternalPhone).HasMaxLength(20);

        e.Property(x => x.Ethnicity).HasMaxLength(50);
        e.Property(x => x.Religion).HasMaxLength(50);
        e.Property(x => x.Nationality).HasMaxLength(50).IsRequired();

        // ===== Giấy tờ =====
        e.Property(x => x.IdCardNumber).HasMaxLength(20);
        e.Property(x => x.IdCardIssuePlace).HasMaxLength(200);
        e.Property(x => x.TaxCode).HasMaxLength(20);
        e.Property(x => x.SocialInsuranceNumber).HasMaxLength(20);
        e.Property(x => x.PassportNumber).HasMaxLength(20);

        // ===== Địa chỉ HKTT (no FK — defer G-H2) =====
        e.Property(x => x.PermanentAddressText).HasMaxLength(500);
        e.Property(x => x.StreetAddressPermanent).HasMaxLength(200);

        // ===== Địa chỉ Tạm trú (no FK — defer G-H2) =====
        e.Property(x => x.TemporaryAddressText).HasMaxLength(500);
        e.Property(x => x.StreetAddressTemporary).HasMaxLength(200);

        // ===== Khẩn cấp =====
        e.Property(x => x.EmergencyContactName).HasMaxLength(200);
        e.Property(x => x.EmergencyContactPhone).HasMaxLength(20);
        e.Property(x => x.EmergencyContactAddress).HasMaxLength(500);

        // ===== Trình độ + chức danh =====
        e.Property(x => x.Qualification).HasMaxLength(200);
        e.Property(x => x.AcademicTitle).HasMaxLength(200);

        // ===== Vị trí =====
        e.Property(x => x.WorkLocation).HasMaxLength(200);
        e.Property(x => x.TimekeepingCode).HasMaxLength(50);

        // ===== Ngân hàng =====
        e.Property(x => x.BankAccount).HasMaxLength(50);
        e.Property(x => x.BankName).HasMaxLength(200);
        e.Property(x => x.BankBranch).HasMaxLength(200);

        // ===== Sức khoẻ =====
        e.Property(x => x.BloodType).HasMaxLength(5);

        // ===== Lương + Phép — decimal precision =====
        e.Property(x => x.BaseSalary).HasColumnType("decimal(18,2)");
        e.Property(x => x.TotalSalary).HasColumnType("decimal(18,2)");
        e.Property(x => x.AnnualLeaveDays).HasColumnType("decimal(5,2)");
        e.Property(x => x.RemainingLeaveDays).HasColumnType("decimal(5,2)");
        e.Property(x => x.CompensatoryLeaveDays).HasColumnType("decimal(5,2)");
        e.Property(x => x.SeniorityLeaveDays).HasColumnType("decimal(5,2)");

        // ===== BHXH =====
        e.Property(x => x.MedicalRegistrationPlace).HasMaxLength(200);

        // ===== Khác =====
        e.Property(x => x.PhotoUrl).HasMaxLength(500);
        e.Property(x => x.Notes).HasMaxLength(2000);

        // ===== Soft delete index =====
        e.HasIndex(x => x.IsDeleted);
    }
}
