using Domain.Entities.Forum.ValueObject;
using Domain.Entities.Forum.Boards;
using Domain.Entities.Forum.Posts;
using Domain.Entities.Members;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace Domain.Entities.Forum.Comments
{
    [Table(nameof(Comment))]
    [Index(nameof(BoardID))]
    [Index(nameof(PostID))]
    [Index(nameof(MemberID))]
    [Index(nameof(ParentID))]

    // 루트 댓글 페이징 & 정렬용
    [Index(nameof(PostID), nameof(IsDeleted), nameof(ParentID), nameof(CreatedAt), IsDescending = new[] { false, false, false, true })]
    [Index(nameof(PostID), nameof(IsDeleted), nameof(ParentID), nameof(Score), nameof(ID), IsDescending = new[] { false, false, false, true, true })]
    [Index(nameof(PostID), nameof(MemberID), nameof(IsDeleted), nameof(ParentID), nameof(CreatedAt), IsDescending = new[] { false, false, false, false, true })]
    [Index(nameof(PostID), nameof(MemberID), nameof(IsDeleted), nameof(ParentID), nameof(Score), nameof(ID), IsDescending = new[] { false, false, false, false, true, true })]

    // 자식 댓글(답글) 등록순
    [Index(nameof(PostID), nameof(IsDeleted), nameof(ParentID), nameof(CreatedAt))]
    [Index(nameof(PostID), nameof(MemberID), nameof(IsDeleted), nameof(ParentID), nameof(CreatedAt))]
    public class Comment
    {
        [ForeignKey(nameof(BoardID))]
        public virtual Board Board { get; set; } = null!;

        [ForeignKey(nameof(PostID))]
        public virtual Post Post { get; set; } = null!;

        [ForeignKey(nameof(MemberID))]
        public virtual Member Member { get; set; } = null!;

        [ForeignKey(nameof(MentionMemberID))]
        public virtual Member? MentionMember { get; set; }

        public virtual Comment? Parent { get; set; }
        public virtual ICollection<Comment> Children { get; set; } = [];

        // 댓글 이미지
        public virtual List<CommentImage> CommentImage { get; set; } = [];

        // 댓글 미디어
        public virtual List<CommentMedia> CommentMedia { get; set; } = [];

        // 댓글 파일
        public virtual List<CommentFile> CommentFile { get; set; } = [];

        // 댓글 링크
        public virtual List<CommentLink> CommentLink { get; set; } = [];

        // 댓글 좋아요/싫어요
        public virtual List<CommentReaction> CommentReaction { get; set; } = [];

        // 댓글 신고
        public virtual List<CommentReport> CommentReport { get; set; } = [];

        // 댓글 파일 다운로드 기록
        public virtual List<CommentFileDownLog> CommentFileDownLog { get; set; } = [];

        // 댓글 링크 클릭 기록
        public virtual List<CommentLinkClickLog> CommentLinkClickLog { get; set; } = [];

        // 댓글 변경 기록
        public virtual List<CommentUpdateLog> CommentUpdateLog { get; set; } = [];

        // 답글 언급
        public virtual CommentMention? CommentMention { get; set; }

        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        [Comment("PK")]
        public int ID { get; set; }

        [Comment("게시판 ID")]
        public int BoardID { get; set; }

        [Comment("게시글 ID")]
        public int PostID { get; set; }

        [Comment("회원 ID")]
        public int MemberID { get; set; }

        [Comment("부모 댓글 ID")]
        public int? ParentID { get; set; }

        [Comment("언급 대상 회원 ID")]
        public int? MentionMemberID { get; set; }

        [Comment("댓글 깊이")]
        public sbyte Depth { get; set; } = 0;

        [Comment("댓글 내용")]
        [StringLength(4000, MinimumLength = 1)]
        public string Content { get; set; } = default!;

        [Comment("회원 SID")]
        [StringLength(20, MinimumLength = 1)]
        public string SID { get; set; } = default!;

        [Comment("회원 이메일")]
        [StringLength(60, MinimumLength = 1)]
        public string Email { get; set; } = default!;

        [Comment("회원 이름")]
        [StringLength(20)]
        public string? Name { get; set; }

        [Comment("1:1문의 답변 여부")]
        public bool IsReply { get; set; } = false;

        [Comment("비밀글 여부")]
        public bool IsSecret { get; set; } = false;

        [Comment("삭제 여부")]
        public bool IsDeleted { get; set; } = false;

        [Comment("좋아요")]
        public int Likes { get; set; } = 0;

        [Comment("싫어요")]
        public int Dislikes { get; set; } = 0;

        [Comment("신고 수")]
        public int Reports { get; set; } = 0;

        [Comment("답글 수")]
        public int Replies { get; set; } = 0;

        [Comment("인기 순위")]
        public int Score { get; set; } = 0; // 정렬을 위한

        [Comment("이미지 수")]
        public int Images { get; set; } = 0;

        [Comment("미디어 수")]
        public byte Medias { get; set; } = 0;

        [Comment("파일 수")]
        public byte Files { get; set; } = 0;

        [Comment("IP Address")]
        [MaxLength(50)]
        public string? IpAddress { get; set; }

        [Comment("User-Agent")]
        [MaxLength(255)]
        public string? UserAgent { get; set; }

        [Comment("상태")]
        [EnumDataType(typeof(DisplayStatus), ErrorMessage = "올바른 상태를 입력해주세요.")]
        public DisplayStatus Status { get; set; } = DisplayStatus.Normal;

        [Comment("삭제 일시")]
        public DateTime? DeletedAt { get; set; }

        [Comment("수정 일시")]
        public DateTime? UpdatedAt { get; set; }

        [Comment("등록 일시")]
        public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
    }
}