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

namespace Application.Features.Api.Forum.Comment.Get;

public sealed class Handler(IAppDbContext db) : IQueryHandler<Query, Result<Response>>
{
    public async Task<Result<Response>> Handle(Query request, CancellationToken ct)
    {
        var item = await db.Comment.AsNoTracking().Include(c => c.Board).Include(c => c.Post).FirstOrDefaultAsync(x => x.ID == request.ID, ct);

        if (item is null)
        {
            return Result.Failure<Response>(Error.NotFound("Comment.NotFound", "댓글을 찾을 수 없습니다."));
        }

        return new Response(
            item.ID,
            item.BoardID,
            item.Board.Name,
            item.PostID,
            item.Post.Subject,
            item.Content,
            item.Name,
            item.SID,
            item.IsReply,
            item.IsSecret,
            item.IsDeleted,
            item.Likes,
            item.Dislikes,
            item.Reports,
            item.Replies,
            item.UpdatedAt,
            item.CreatedAt
        );
    }
}