using Application.Abstractions.Data;
using Application.Abstractions.Messaging;
using Microsoft.EntityFrameworkCore;

namespace Application.Features.Admin.Forum.Trash.Comment.PermanentDelete;

public sealed class Handler(IAppDbContext db) : ICommandHandler<Command>
{
    public async Task Handle(Command request, CancellationToken ct)
    {
        if (request.IDs is null || request.IDs.Length == 0)
        {
            return;
        }

        var comments = await db.Comment.Where(c => request.IDs.Contains(c.ID) && c.IsDeleted).ToListAsync(ct);
        if (comments.Count == 0)
        {
            return;
        }

        // 자식 댓글(대댓글) 재귀 조회 → 삭제 대상에 포함
        var allIDs = comments.Select(c => c.ID).ToHashSet();
        var parentIDs = allIDs.ToList();

        while (parentIDs.Count > 0)
        {
            var childComments = await db.Comment.Where(c => c.ParentID != null && parentIDs.Contains(c.ParentID.Value) && !allIDs.Contains(c.ID)).ToListAsync(ct);
            if (childComments.Count == 0)
            {
                break;
            }

            parentIDs = [.. childComments.Select(c => c.ID)];
            allIDs.UnionWith(parentIDs);
            comments.AddRange(childComments);
        }

        var allIDList = allIDs.ToList();

        // 로그 테이블 선삭제 (Restrict FK 충돌 방지)
        var fileDownLogs = await db.CommentFileDownLog.Where(c => allIDList.Contains(c.CommentID)).ToListAsync(ct);
        if (fileDownLogs.Count > 0)
        {
            db.CommentFileDownLog.RemoveRange(fileDownLogs);
        }

        var linkClickLogs = await db.CommentLinkClickLog.Where(c => allIDList.Contains(c.CommentID)).ToListAsync(ct);
        if (linkClickLogs.Count > 0)
        {
            db.CommentLinkClickLog.RemoveRange(linkClickLogs);
        }

        // 자식 먼저 삭제되도록 Depth 역순 정렬
        var orderedComments = comments.OrderByDescending(c => c.Depth).ToList();
        db.Comment.RemoveRange(orderedComments);

        await db.SaveChangesAsync(ct);
    }
}