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

namespace BlogPlatform.Endpoints.Post;

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

public class GetPostEndpoint(BlogPlatformDbContext db) : Endpoint<GetPostRequest,GetPostDto>
{
    public override void Configure()
    {
        Get("/api/posts/{@id}", x => new{x.Id});
    }

    public override async Task HandleAsync(GetPostRequest req, CancellationToken ct)
    {
        Models.Post post = await db.Posts
            .Include(post => post.User)
            .SingleOrDefaultAsync(x => x.Id == req.Id);

        if (post == null)
        {
            await Send.NotFoundAsync(ct);
            return;
        }
        
        GetPostDto dto = new()
        {
            Id = post.Id,
            Title = req.Title,
            Content = req.Content,
            Likes = req.Likes,
            UserId = req.UserId,
            UserUsername = checkingUser.Username,
            UserEmail = checkingUser.Email,
        };

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