using CMSMicroservice.Domain.Entities.Content;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace CMSMicroservice.Infrastructure.Persistence.Configurations;

public class SitePageImageConfiguration : IEntityTypeConfiguration<SitePageImage>
{
    public void Configure(EntityTypeBuilder<SitePageImage> builder)
    {
        builder.ToTable("SitePageImages");
        builder.HasKey(x => x.Id);

        builder.Property(x => x.SitePageSettingsId)
            .IsRequired();

        builder.Property(x => x.ImageGroup)
            .IsRequired()
            .HasMaxLength(50);

        builder.Property(x => x.Title)
            .HasMaxLength(200);

        builder.Property(x => x.Subtitle)
            .HasMaxLength(300);

        builder.Property(x => x.ImagePath)
            .IsRequired();

        builder.Property(x => x.IconName)
            .HasMaxLength(100);

        builder.Property(x => x.LinkUrl)
            .HasMaxLength(500);

        builder.Property(x => x.SortOrder)
            .IsRequired()
            .HasDefaultValue(0);

        builder.Property(x => x.IsActive)
            .IsRequired()
            .HasDefaultValue(true);

        // رابطه با SitePageSettings
        builder.HasOne(x => x.SitePageSettings)
            .WithMany(x => x.Images)
            .HasForeignKey(x => x.SitePageSettingsId)
            .OnDelete(DeleteBehavior.Cascade);

        // ایندکس ترکیبی
        builder.HasIndex(x => new { x.SitePageSettingsId, x.ImageGroup })
            .HasDatabaseName("IX_SitePageImages_SettingsId_Group");
    }
}
