using System.Security.Claims;
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using TrackQrApi.Data;
using TrackQrApi.Features.Auth.Common;
using TrackQrApi.Features.Projects.Common;

namespace TrackQrApi.Features.Projects.Endpoints;

public class ListProjectsRequest
{
    public Guid WorkspaceId { get; set; }
}

public class ListProjectsEndpoint(AppDbContext db)
    : Endpoint<ListProjectsRequest, ProjectListResponse>
{
    public override void Configure()
    {
        Get("/workspaces/{WorkspaceId}/projects");
    }

    public override async Task HandleAsync(ListProjectsRequest req, CancellationToken ct)
    {
        var userId = Guid.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier)!);

        // Verify workspace ownership
        var workspaceExists = await db.Workspaces
            .AnyAsync(w => w.Id == req.WorkspaceId && w.OwnerUserId == userId, ct);

        if (!workspaceExists)
        {
            await HttpContext.Response.SendAsync(new MessageResponse("Workspace not found"), 404, cancellation: ct);
            return;
        }

        var projects = await db.Projects
            .Where(p => p.WorkspaceId == req.WorkspaceId)
            .OrderByDescending(p => p.CreatedAt)
            .Select(p => new ProjectResponse(
                p.Id,
                p.WorkspaceId,
                p.Name,
                p.Description,
                p.ShortLinks.Count(l => l.DeletedAt == null),
                p.QRCodeDesigns.Count,
                p.CreatedAt
            ))
            .ToListAsync(ct);

        await HttpContext.Response.SendAsync(new ProjectListResponse(projects), cancellation: ct);
    }
}