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

namespace Infrastructure.Persistence.Configurations.News;

public sealed class RssNewsArticleConfiguration : IEntityTypeConfiguration<RssNewsArticle>
{
    public void Configure(EntityTypeBuilder<RssNewsArticle> builder)
    {
        builder.HasIndex(x => new { x.Guid, x.RssFeedSourceID }).IsUnique().HasFilter("[Guid] IS NOT NULL");
        builder.HasIndex(x => x.PublishedAt);
        builder.HasIndex(x => x.RssFeedSourceID);
        builder.HasIndex(x => x.CreatedAt);

        builder.ToTable(nameof(RssNewsArticle), t => t.HasComment("RSS 뉴스 기사"));
        builder.HasKey(x => x.ID);
        builder.Property(x => x.ID).ValueGeneratedOnAdd().HasComment("PK");
        builder.Property(x => x.RssFeedSourceID).IsRequired().HasComment("RSS 피드 소스 FK");
        builder.Property(x => x.Title).HasMaxLength(500).IsRequired().HasComment("기사 제목");
        builder.Property(x => x.Link).HasMaxLength(2000).HasComment("원본 링크");
        builder.Property(x => x.Guid).HasMaxLength(1000).HasComment("RSS GUID (중복 방지)");
        builder.Property(x => x.Author).HasMaxLength(200).HasComment("작성자");
        builder.Property(x => x.Description).HasColumnType("nvarchar(max)").HasComment("요약");
        builder.Property(x => x.Content).HasColumnType("nvarchar(max)").HasComment("본문 (content:encoded)");
        builder.Property(x => x.ImageUrl).HasMaxLength(2000).HasComment("썸네일 이미지 URL");
        builder.Property(x => x.SourceName).HasMaxLength(200).HasComment("출처명");
        builder.Property(x => x.Categories).HasColumnType("nvarchar(max)").HasComment("카테고리 (JSON 배열)");
        builder.Property(x => x.CommentCount).IsRequired().HasComment("댓글 수");
        builder.Property(x => x.PublishedAt).HasComment("발행 일시");
        builder.Property(x => x.CreatedAt).IsRequired().HasComment("수집 일시");
    }
}