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);

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

        var boardPostCounts = posts.GroupBy(p => p.BoardID).ToDictionary(g => g.Key, g => g.Count());
        var boards = await db.Board.Where(b => boardPostCounts.Keys.Contains(b.ID)).ToListAsync(ct);
        foreach (var board in boards)
        {
            board.Posts -= boardPostCounts[board.ID];
            board.UpdatedAt = DateTime.UtcNow;
        }

        var boardGroupIDs = boards.Select(c => c.BoardGroupID).Distinct().ToList();
        var boardGroups = await db.BoardGroup.Where(g => boardGroupIDs.Contains(g.ID)).ToListAsync(ct);

        var boardToGroup = boards.ToDictionary(b => b.ID, b => b.BoardGroupID);
        var groupPostCounts = posts.GroupBy(p => boardToGroup[p.BoardID]).ToDictionary(g => g.Key, g => g.Count());

        foreach (var group in boardGroups)
        {
            if (groupPostCounts.TryGetValue(group.ID, out var count))
            {
                group.Posts -= count;
                group.UpdatedAt = DateTime.UtcNow;
            }
        }

        db.Post.RemoveRange(posts);

        await db.SaveChangesAsync(ct);
    }
}