using Domain.Entities.Forum.Posts;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Infrastructure.Persistence.Configurations.Forum.Posts;

public sealed class PostImageConfiguration : IEntityTypeConfiguration<PostImage>
{
    public void Configure(EntityTypeBuilder<PostImage> builder)
    {
        builder.HasIndex(x => x.BoardID);
        builder.HasIndex(x => x.PostID);
        builder.HasIndex(x => x.UUID).IsUnique();
        builder.HasIndex(x => new { x.PostID, x.HashedName });
        builder.HasIndex(x => new { x.PostID, x.HashedName, x.IsDisabled });

        builder.HasOne(x => x.Board).WithMany().HasForeignKey(x => x.BoardID).OnDelete(DeleteBehavior.Restrict);
        builder.HasOne(x => x.Post).WithMany(x => x.PostImage).HasForeignKey(x => x.PostID).OnDelete(DeleteBehavior.Cascade);

        builder.ToTable("PostImage", x => x.HasComment("게시글 이미지"));
        builder.HasKey(x => x.ID);
        builder.Property(x => x.ID).ValueGeneratedOnAdd().HasComment("PK");
        builder.Property(x => x.BoardID).IsRequired().HasComment("게시판 ID");
        builder.Property(x => x.PostID).IsRequired().HasComment("게시글 ID");
        builder.Property(x => x.UUID).IsRequired().HasComment("이미지 ID");
        builder.Property(x => x.FileName).HasMaxLength(255).IsRequired().HasComment("원본 파일명");
        builder.Property(x => x.HashedName).HasMaxLength(255).IsRequired().HasComment("저장 파일명");
        builder.Property(x => x.Path).HasMaxLength(500).IsRequired().HasComment("저장 경로");
        builder.Property(x => x.Url).HasMaxLength(1000).IsRequired().HasComment("URL");
        builder.Property(x => x.Extension).HasMaxLength(10).HasComment("확장자");
        builder.Property(x => x.ContentType).HasMaxLength(100).HasComment("MIME 타입");
        builder.Property(x => x.Size).HasComment("용량(byte)");
        builder.Property(x => x.Width).HasComment("가로 해상도(px)");
        builder.Property(x => x.Height).HasComment("세로 해상도(px)");
        builder.Property(x => x.IsDisabled).IsRequired().HasComment("비활성 여부");
        builder.Property(x => x.DisabledAt).HasComment("비활성 일시");
        builder.Property(x => x.CreatedAt).IsRequired().HasComment("등록 일시");
    }
}
