using BlogPlatform.DTO.Comment.Response;
using FastEndpoints;
using Microsoft.EntityFrameworkCore;

namespace BlogPlatform.Endpoints.Comment;


public class GetCommentRequest
{
    public int Id { get; set; }
}


public class GetCommentEndpoint(BlogPlatformDbContext db) : Endpoint<GetCommentRequest,GetCommentDto>
{
    public override void Configure()
    {
        Get("/api/comments/{@id}", x => new{x.Id});
    }

    public override async Task HandleAsync(GetCommentRequest req, CancellationToken ct)
    {
        Models.Comment? comment = await db.Comments
            .Include(comment => comment.User)
            .Include(comment => comment.Post)
            .FirstOrDefaultAsync(x => x.Id == req.Id);

        if (comment == null)
        {
            await Send.NotFoundAsync(ct);
            return;
        }

        // Returning the response with DTO
        GetCommentDto dto = new()
        {
            Id = comment.Id,
            Content = comment.Content,
            PostId = comment.PostId,
            PostTitle = comment.Post.Title,
            UserId = comment.UserId,
            UserUsername = comment.User.Username

        };

        await Send.OkAsync(dto, ct);
    }
}
