using Lada.Framework.Data.Domains.Premises;
using Lada.Framework.Data.Mappings;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Lada.Framework.Data.Mappings.EntityMappings.Premises
{
    public class PremiseMap : IEntityMappingConfiguration<Premise>
    {
        public void Map(EntityTypeBuilder<Premise> builder)
        {
            builder.ToTable("Premises");

            // Primary Key
            builder.HasKey(x => x.Id);

            // Properties
            builder.Property(x => x.Code).HasMaxLength(50);
            builder.Property(x => x.Status).IsRequired();
            builder.Property(x => x.AgencyId).IsRequired();
            builder.Property(x => x.CityId).IsRequired();
            builder.Property(x => x.DistrictId).IsRequired();
            builder.Property(x => x.WardId).IsRequired();
            builder.Property(x => x.AddressDetail).HasMaxLength(500).IsRequired();
            builder.Property(x => x.GoogleMapUrl).HasMaxLength(1000);
            builder.Property(x => x.Description).HasMaxLength(4000);

            // Relationships
            builder.HasOne(x => x.Agency)
                .WithMany()
                .HasForeignKey(x => x.AgencyId)
                .OnDelete(DeleteBehavior.Restrict);

            builder.HasOne(x => x.City)
                .WithMany()
                .HasForeignKey(x => x.CityId)
                .OnDelete(DeleteBehavior.Restrict);

            builder.HasOne(x => x.District)
                .WithMany()
                .HasForeignKey(x => x.DistrictId)
                .OnDelete(DeleteBehavior.Restrict);

            builder.HasOne(x => x.Ward)
                .WithMany()
                .HasForeignKey(x => x.WardId)
                .OnDelete(DeleteBehavior.Restrict);

            // Indexes
            builder.HasIndex(x => x.AgencyId)
                .HasDatabaseName("IX_Premises_AgencyId");

            builder.HasIndex(x => x.CityId)
                .HasDatabaseName("IX_Premises_CityId");

            builder.HasIndex(x => x.DistrictId)
                .HasDatabaseName("IX_Premises_DistrictId");

            builder.HasIndex(x => x.WardId)
                .HasDatabaseName("IX_Premises_WardId");

            builder.HasIndex(x => x.Status)
                .HasDatabaseName("IX_Premises_Status");

            builder.HasIndex(x => x.TotalScore)
                .HasDatabaseName("IX_Premises_TotalScore");

            builder.HasIndex(x => x.Code)
                .HasDatabaseName("IX_Premises_Code");
        }
    }
}
