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

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

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

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

        foreach (var post in posts)
        {
            if (!string.IsNullOrEmpty(post.Thumbnail))
            {
                fileStorage.DeleteByUrl(post.Thumbnail);
            }
        }

        var postIDs = posts.Select(c => c.ID).ToList();

        // 로그 테이블 선삭제 (Restrict FK 충돌 방지)
        // Post 로그
        var postFileDownLogs = await db.PostFileDownLog.Where(c => postIDs.Contains(c.PostID)).ToListAsync(ct);
        if (postFileDownLogs.Count > 0)
        {
            db.PostFileDownLog.RemoveRange(postFileDownLogs);
        }

        var postLinkClickLogs = await db.PostLinkClickLog.Where(c => postIDs.Contains(c.PostID)).ToListAsync(ct);
        if (postLinkClickLogs.Count > 0)
        {
            db.PostLinkClickLog.RemoveRange(postLinkClickLogs);
        }

        // Comment 로그 (Post → Comment Cascade 시 충돌 방지)
        var commentIDs = await db.Comment.Where(c => postIDs.Contains(c.PostID)).Select(c => c.ID).ToListAsync(ct);
        if (commentIDs.Count > 0)
        {
            var commentFileDownLogs = await db.CommentFileDownLog.Where(c => commentIDs.Contains(c.CommentID)).ToListAsync(ct);
            if (commentFileDownLogs.Count > 0)
            {
                db.CommentFileDownLog.RemoveRange(commentFileDownLogs);
            }

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

        db.Post.RemoveRange(posts);

        await db.SaveChangesAsync(ct);
    }
}