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

namespace Infrastructure.Persistence.Configurations.News;

public sealed class RssFeedSourceConfiguration : IEntityTypeConfiguration<RssFeedSource>
{
    public void Configure(EntityTypeBuilder<RssFeedSource> builder)
    {
        builder.HasIndex(x => x.Url).IsUnique();
        builder.HasIndex(x => x.IsActive);

        builder.HasMany(x => x.RssNewsArticle).WithOne(x => x.RssFeedSource).HasForeignKey(x => x.RssFeedSourceID).OnDelete(DeleteBehavior.Cascade);

        builder.ToTable(nameof(RssFeedSource), t => t.HasComment("RSS 피드 소스"));
        builder.HasKey(x => x.ID);
        builder.Property(x => x.ID).ValueGeneratedOnAdd().HasComment("PK");
        builder.Property(x => x.Name).HasMaxLength(200).IsRequired().HasComment("소스 이름");
        builder.Property(x => x.Url).HasMaxLength(1000).IsRequired().HasComment("RSS 피드 URL");
        builder.Property(x => x.Description).HasColumnType("nvarchar(max)").HasComment("소스 설명");
        builder.Property(x => x.IntervalMinutes).IsRequired().HasComment("수집 주기 (분)");
        builder.Property(x => x.IsActive).IsRequired().HasComment("활성화 여부");
        builder.Property(x => x.LastFetchedAt).HasComment("마지막 수집 일시");
        builder.Property(x => x.UpdatedAt).HasComment("수정 일시");
        builder.Property(x => x.CreatedAt).IsRequired().HasComment("등록 일시");
    }
}