using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using SolutionErp.Domain.Contracts.Details;

namespace SolutionErp.Infrastructure.Persistence.Configurations;

// EF config cho 7 ContractType-specific Details bảng + 1 base abstract.
// Common pattern: ContractId FK Cascade, Order int, ThanhTien decimal(18,2),
// GhiChu nvarchar(1000), index theo (ContractId, Order).

public class ThauPhuDetailConfiguration : IEntityTypeConfiguration<ThauPhuDetail>
{
    public void Configure(EntityTypeBuilder<ThauPhuDetail> b)
    {
        b.ToTable("ThauPhuDetails");
        b.HasKey(x => x.Id);

        b.Property(x => x.HangMuc).HasMaxLength(500).IsRequired();
        b.Property(x => x.DonViTinh).HasMaxLength(50).IsRequired();
        b.Property(x => x.KhoiLuong).HasPrecision(18, 4);
        b.Property(x => x.DonGia).HasPrecision(18, 2);
        b.Property(x => x.ThanhTien).HasPrecision(18, 2);
        b.Property(x => x.GhiChu).HasMaxLength(1000);

        b.HasOne(x => x.Contract)
            .WithMany(c => c.ThauPhuDetails)
            .HasForeignKey(x => x.ContractId)
            .OnDelete(DeleteBehavior.Cascade);

        b.HasIndex(x => new { x.ContractId, x.Order });
    }
}

public class GiaoKhoanDetailConfiguration : IEntityTypeConfiguration<GiaoKhoanDetail>
{
    public void Configure(EntityTypeBuilder<GiaoKhoanDetail> b)
    {
        b.ToTable("GiaoKhoanDetails");
        b.HasKey(x => x.Id);

        b.Property(x => x.MaCongViec).HasMaxLength(100).IsRequired();
        b.Property(x => x.TenCongViec).HasMaxLength(500).IsRequired();
        b.Property(x => x.DonViTinh).HasMaxLength(50).IsRequired();
        b.Property(x => x.KhoiLuong).HasPrecision(18, 4);
        b.Property(x => x.DonGia).HasPrecision(18, 2);
        b.Property(x => x.ThanhTien).HasPrecision(18, 2);
        b.Property(x => x.GhiChu).HasMaxLength(1000);
        b.Property(x => x.YeuCauKyThuat).HasMaxLength(2000);

        b.HasOne(x => x.Contract)
            .WithMany(c => c.GiaoKhoanDetails)
            .HasForeignKey(x => x.ContractId)
            .OnDelete(DeleteBehavior.Cascade);

        b.HasIndex(x => new { x.ContractId, x.Order });
    }
}

public class NhaCungCapDetailConfiguration : IEntityTypeConfiguration<NhaCungCapDetail>
{
    public void Configure(EntityTypeBuilder<NhaCungCapDetail> b)
    {
        b.ToTable("NhaCungCapDetails");
        b.HasKey(x => x.Id);

        b.Property(x => x.MaSP).HasMaxLength(100).IsRequired();
        b.Property(x => x.TenSP).HasMaxLength(500).IsRequired();
        b.Property(x => x.ThongSoKyThuat).HasMaxLength(2000);
        b.Property(x => x.DonViTinh).HasMaxLength(50).IsRequired();
        b.Property(x => x.SoLuong).HasPrecision(18, 4);
        b.Property(x => x.DonGia).HasPrecision(18, 2);
        b.Property(x => x.ThanhTien).HasPrecision(18, 2);
        b.Property(x => x.GhiChu).HasMaxLength(1000);
        b.Property(x => x.XuatXu).HasMaxLength(100);

        b.HasOne(x => x.Contract)
            .WithMany(c => c.NhaCungCapDetails)
            .HasForeignKey(x => x.ContractId)
            .OnDelete(DeleteBehavior.Cascade);

        b.HasIndex(x => new { x.ContractId, x.Order });
    }
}

public class DichVuDetailConfiguration : IEntityTypeConfiguration<DichVuDetail>
{
    public void Configure(EntityTypeBuilder<DichVuDetail> b)
    {
        b.ToTable("DichVuDetails");
        b.HasKey(x => x.Id);

        b.Property(x => x.MaDichVu).HasMaxLength(100).IsRequired();
        b.Property(x => x.TenDichVu).HasMaxLength(500).IsRequired();
        b.Property(x => x.MoTa).HasMaxLength(2000);
        b.Property(x => x.DonViTinh).HasMaxLength(50).IsRequired();
        b.Property(x => x.ThoiGian).HasPrecision(18, 4);
        b.Property(x => x.DonGia).HasPrecision(18, 2);
        b.Property(x => x.ThanhTien).HasPrecision(18, 2);
        b.Property(x => x.GhiChu).HasMaxLength(1000);

        b.HasOne(x => x.Contract)
            .WithMany(c => c.DichVuDetails)
            .HasForeignKey(x => x.ContractId)
            .OnDelete(DeleteBehavior.Cascade);

        b.HasIndex(x => new { x.ContractId, x.Order });
    }
}

public class MuaBanDetailConfiguration : IEntityTypeConfiguration<MuaBanDetail>
{
    public void Configure(EntityTypeBuilder<MuaBanDetail> b)
    {
        b.ToTable("MuaBanDetails");
        b.HasKey(x => x.Id);

        b.Property(x => x.MaSP).HasMaxLength(100).IsRequired();
        b.Property(x => x.TenSP).HasMaxLength(500).IsRequired();
        b.Property(x => x.MoTa).HasMaxLength(2000);
        b.Property(x => x.DonViTinh).HasMaxLength(50).IsRequired();
        b.Property(x => x.SoLuong).HasPrecision(18, 4);
        b.Property(x => x.DonGia).HasPrecision(18, 2);
        b.Property(x => x.ThueVAT).HasPrecision(5, 2);            // %
        b.Property(x => x.ThanhTien).HasPrecision(18, 2);
        b.Property(x => x.GhiChu).HasMaxLength(1000);
        b.Property(x => x.XuatXu).HasMaxLength(100);

        b.HasOne(x => x.Contract)
            .WithMany(c => c.MuaBanDetails)
            .HasForeignKey(x => x.ContractId)
            .OnDelete(DeleteBehavior.Cascade);

        b.HasIndex(x => new { x.ContractId, x.Order });
    }
}

public class NguyenTacNccDetailConfiguration : IEntityTypeConfiguration<NguyenTacNccDetail>
{
    public void Configure(EntityTypeBuilder<NguyenTacNccDetail> b)
    {
        b.ToTable("NguyenTacNccDetails");
        b.HasKey(x => x.Id);

        b.Property(x => x.NhomSP).HasMaxLength(200).IsRequired();
        b.Property(x => x.TenSP).HasMaxLength(500).IsRequired();
        b.Property(x => x.DonViTinh).HasMaxLength(50).IsRequired();
        b.Property(x => x.DonGiaToiThieu).HasPrecision(18, 2);
        b.Property(x => x.DonGiaToiDa).HasPrecision(18, 2);
        b.Property(x => x.ThanhTien).HasPrecision(18, 2);
        b.Property(x => x.DieuKienGiaoHang).HasMaxLength(1000);
        b.Property(x => x.DieuKienThanhToan).HasMaxLength(1000);
        b.Property(x => x.GhiChu).HasMaxLength(1000);

        b.HasOne(x => x.Contract)
            .WithMany(c => c.NguyenTacNccDetails)
            .HasForeignKey(x => x.ContractId)
            .OnDelete(DeleteBehavior.Cascade);

        b.HasIndex(x => new { x.ContractId, x.Order });
    }
}

public class NguyenTacDvDetailConfiguration : IEntityTypeConfiguration<NguyenTacDvDetail>
{
    public void Configure(EntityTypeBuilder<NguyenTacDvDetail> b)
    {
        b.ToTable("NguyenTacDvDetails");
        b.HasKey(x => x.Id);

        b.Property(x => x.LoaiDichVu).HasMaxLength(200).IsRequired();
        b.Property(x => x.TenDichVu).HasMaxLength(500).IsRequired();
        b.Property(x => x.DonViTinh).HasMaxLength(50).IsRequired();
        b.Property(x => x.DonGiaToiThieu).HasPrecision(18, 2);
        b.Property(x => x.DonGiaToiDa).HasPrecision(18, 2);
        b.Property(x => x.ThanhTien).HasPrecision(18, 2);
        b.Property(x => x.PhamViDichVu).HasMaxLength(1000);
        b.Property(x => x.SLA).HasMaxLength(500);
        b.Property(x => x.GhiChu).HasMaxLength(1000);

        b.HasOne(x => x.Contract)
            .WithMany(c => c.NguyenTacDvDetails)
            .HasForeignKey(x => x.ContractId)
            .OnDelete(DeleteBehavior.Cascade);

        b.HasIndex(x => new { x.ContractId, x.Order });
    }
}
