using Microsoft.AspNetCore.Http.HttpResults;
using WorkClub.Api.Services;
using WorkClub.Application.Clubs.DTOs;

namespace WorkClub.Api.Endpoints.Clubs;

public static class ClubEndpoints
{
    public static void MapClubEndpoints(this IEndpointRouteBuilder app)
    {
        var group = app.MapGroup("/api/clubs");

        group.MapGet("/me", GetMyClubs)
            .RequireAuthorization("RequireViewer")
            .WithName("GetMyClubs");

        group.MapGet("/current", GetCurrentClub)
            .RequireAuthorization("RequireMember")
            .WithName("GetCurrentClub");
    }

    private static async Task<Ok<List<ClubListDto>>> GetMyClubs(ClubService clubService)
    {
        var result = await clubService.GetMyClubsAsync();
        return TypedResults.Ok(result);
    }

    private static async Task<Results<Ok<ClubDetailDto>, NotFound>> GetCurrentClub(ClubService clubService)
    {
        var result = await clubService.GetCurrentClubAsync();

        if (result == null)
            return TypedResults.NotFound();

        return TypedResults.Ok(result);
    }
}
