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

namespace CMSMicroservice.Infrastructure.Persistence.Configurations;

public class PublicMessageConfiguration : IEntityTypeConfiguration<PublicMessage>
{
    public void Configure(EntityTypeBuilder<PublicMessage> builder)
    {
        builder.HasQueryFilter(p => !p.IsDeleted);
        builder.ToTable("PublicMessages");

        builder.HasKey(x => x.Id);

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

        builder.Property(x => x.Content)
            .IsRequired()
            .HasMaxLength(2000);

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

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

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

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

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

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

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

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

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

        // Indexes
        builder.HasIndex(x => x.IsActive)
            .HasDatabaseName("IX_PublicMessages_IsActive");

        builder.HasIndex(x => x.Type)
            .HasDatabaseName("IX_PublicMessages_Type");

        builder.HasIndex(x => x.Priority)
            .HasDatabaseName("IX_PublicMessages_Priority");

        builder.HasIndex(x => x.StartsAt)
            .HasDatabaseName("IX_PublicMessages_StartsAt");

        builder.HasIndex(x => x.ExpiresAt)
            .HasDatabaseName("IX_PublicMessages_ExpiresAt");

        builder.HasIndex(x => x.CreatedByUserId)
            .HasDatabaseName("IX_PublicMessages_CreatedByUserId");

        builder.HasIndex(x => new { x.IsActive, x.ExpiresAt })
            .HasDatabaseName("IX_PublicMessages_IsActive_ExpiresAt");
    }
}
