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

namespace CMSMicroservice.Infrastructure.Persistence.Configurations.Blog;

public class BlogPostConfiguration : IEntityTypeConfiguration<BlogPost>
{
    public void Configure(EntityTypeBuilder<BlogPost> builder)
    {
        builder.ToTable("BlogPosts");
        builder.HasKey(x => x.Id);

        builder.Property(x => x.Title).IsRequired().HasMaxLength(200);
        builder.Property(x => x.Slug).IsRequired().HasMaxLength(200);
        builder.Property(x => x.Summary).HasMaxLength(500);
        builder.Property(x => x.HtmlContent).IsRequired();
        builder.Property(x => x.FeaturedImagePath);
        builder.Property(x => x.FeaturedImageThumbnailPath);
        builder.Property(x => x.Status).IsRequired();
        builder.Property(x => x.ViewCount).IsRequired().HasDefaultValue(0);
        builder.Property(x => x.AuthorUserId).IsRequired();
        builder.Property(x => x.IsFeatured).IsRequired().HasDefaultValue(false);
        builder.Property(x => x.SortOrder).IsRequired().HasDefaultValue(0);

        // Indexes
        builder.HasIndex(x => x.Slug).IsUnique().HasDatabaseName("IX_BlogPosts_Slug");
        builder.HasIndex(x => x.Status).HasDatabaseName("IX_BlogPosts_Status");
        builder.HasIndex(x => x.PublishedAt).HasDatabaseName("IX_BlogPosts_PublishedAt");
        builder.HasIndex(x => x.IsFeatured).HasDatabaseName("IX_BlogPosts_IsFeatured");
        builder.HasIndex(x => x.AuthorUserId).HasDatabaseName("IX_BlogPosts_AuthorUserId");
        builder.HasIndex(x => new { x.Status, x.PublishedAt })
            .HasDatabaseName("IX_BlogPosts_Status_PublishedAt");
    }
}
