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

namespace Infrastructure.Persistence.Configurations.Forum.Posts;

public sealed class PostConfiguration : IEntityTypeConfiguration<Post>
{
    public void Configure(EntityTypeBuilder<Post> builder)
    {
        builder.HasIndex(x => x.BoardID);
        builder.HasIndex(x => x.BoardPrefixID);
        builder.HasIndex(x => x.MemberID);
        builder.HasIndex(x => new { x.ID, x.BoardID });
        builder.HasIndex(x => new { x.ID, x.BoardID, x.IsDeleted });
        builder.HasIndex(x => new { x.ID, x.BoardID, x.BoardPrefixID, x.IsDeleted, x.CreatedAt });
        builder.HasIndex(x => new { x.ID, x.BoardID, x.BoardPrefixID, x.IsDeleted, x.Views });
        builder.HasIndex(x => new { x.ID, x.BoardID, x.BoardPrefixID, x.IsDeleted, x.Comments });
        builder.HasIndex(x => new { x.ID, x.BoardID, x.BoardPrefixID, x.IsDeleted, x.Likes });

        builder.HasOne(x => x.Board).WithMany(x => x.Post).HasForeignKey(x => x.BoardID).OnDelete(DeleteBehavior.Restrict);
        builder.HasOne(x => x.BoardPrefix).WithMany().HasForeignKey(x => x.BoardPrefixID).OnDelete(DeleteBehavior.SetNull);
        builder.HasOne(x => x.Member).WithMany().HasForeignKey(x => x.MemberID).IsRequired(false).OnDelete(DeleteBehavior.SetNull);
        builder.HasMany(x => x.Comment).WithOne(x => x.Post).HasForeignKey(x => x.PostID).OnDelete(DeleteBehavior.Cascade);
        builder.HasMany(x => x.PostImage).WithOne(x => x.Post).HasForeignKey(x => x.PostID).OnDelete(DeleteBehavior.Cascade);
        builder.HasMany(x => x.PostMedia).WithOne(x => x.Post).HasForeignKey(x => x.PostID).OnDelete(DeleteBehavior.Cascade);
        builder.HasMany(x => x.PostFile).WithOne(x => x.Post).HasForeignKey(x => x.PostID).OnDelete(DeleteBehavior.Cascade);
        builder.HasMany(x => x.PostLink).WithOne(x => x.Post).HasForeignKey(x => x.PostID).OnDelete(DeleteBehavior.Cascade);
        builder.HasMany(x => x.PostTag).WithOne(x => x.Post).HasForeignKey(x => x.PostID).OnDelete(DeleteBehavior.Cascade);
        builder.HasMany(x => x.PostReaction).WithOne(x => x.Post).HasForeignKey(x => x.PostID).OnDelete(DeleteBehavior.Cascade);
        builder.HasMany(x => x.PostBookmark).WithOne(x => x.Post).HasForeignKey(x => x.PostID).OnDelete(DeleteBehavior.Cascade);
        builder.HasMany(x => x.PostReport).WithOne(x => x.Post).HasForeignKey(x => x.PostID).OnDelete(DeleteBehavior.Cascade);

        builder.ToTable(nameof(Post), 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.BoardPrefixID).HasComment("게시글 말머리 ID");
        builder.Property(x => x.MemberID).HasComment("회원 ID");
        builder.Property(x => x.Subject).HasMaxLength(255).IsRequired().HasComment("제목");
        builder.Property(x => x.Content).HasMaxLength(8000).IsRequired().HasComment("내용");
        builder.Property(x => x.SID).HasMaxLength(20).HasComment("회원 SID");
        builder.Property(x => x.Email).HasMaxLength(60).HasComment("회원 이메일");
        builder.Property(x => x.Name).HasMaxLength(20).HasComment("회원 이름");
        builder.Property(x => x.Thumbnail).HasMaxLength(255).HasComment("대표 이미지");
        builder.Property(x => x.IsReply).IsRequired().HasComment("답변 여부");
        builder.Property(x => x.IsAnonymous).IsRequired().HasComment("익명 글 여부");
        builder.Property(x => x.IsSecret).IsRequired().HasComment("비밀글 여부");
        builder.Property(x => x.IsNotice).IsRequired().HasComment("일반 공지 여부");
        builder.Property(x => x.IsSpeaker).IsRequired().HasComment("전체 공지 여부");
        builder.Property(x => x.IsDeleted).IsRequired().HasComment("삭제 여부");
        builder.Property(x => x.Views).IsRequired().HasComment("조회 수");
        builder.Property(x => x.Likes).IsRequired().HasComment("좋아요");
        builder.Property(x => x.Dislikes).IsRequired().HasComment("싫어요");
        builder.Property(x => x.Comments).IsRequired().HasComment("댓글 수");
        builder.Property(x => x.Bookmarks).IsRequired().HasComment("즐겨찾기 수");
        builder.Property(x => x.Reports).IsRequired().HasComment("신고 수");
        builder.Property(x => x.Images).IsRequired().HasComment("이미지 수");
        builder.Property(x => x.Medias).IsRequired().HasComment("미디어 수");
        builder.Property(x => x.Files).IsRequired().HasComment("파일 수");
        builder.Property(x => x.Tags).IsRequired().HasComment("Tag 수");
        builder.Property(x => x.IpAddress).HasMaxLength(50).HasComment("IP");
        builder.Property(x => x.UserAgent).HasMaxLength(255).HasComment("User-Agent");
        builder.Property(x => x.LastReplyUpdatedAt).HasComment("마지막 답변 일시");
        builder.Property(x => x.LastCommentUpdatedAt).HasComment("마지막 댓글 일시");
        builder.Property(x => x.DeletedAt).HasComment("삭제 일시");
        builder.Property(x => x.UpdatedAt).HasComment("수정 일시");
        builder.Property(x => x.CreatedAt).IsRequired().HasComment("등록 일시");
    }
}
