namespace Backend.Endpoints;

public static partial class AdminSeasonManagementEndpoints
{
    public static RouteGroupBuilder MapAdminSeasonManagementEndpoints(this RouteGroupBuilder group)
    {
        group.MapGet("/seasons", GetSeasons)
            .AddEndpointFilter((context, next) => AdminEndpointConventions.RequireAnyPermission(context, next, Backend.Security.AdminPermissionCatalog.SeasonReadPermissionKeys))
            .WithName("GetAdminSeasons")
            .WithOpenApi();
        group.MapPost("/seasons", CreateSeason)
            .AddEndpointFilter((context, next) => AdminEndpointConventions.RequireWritePermission(context, next, Backend.Security.AdminPermissionCatalog.Years))
            .WithName("CreateAdminSeason")
            .WithOpenApi();
        group.MapGet("/seasons/{seasonId:int}", GetSeasonDetail)
            .AddEndpointFilter((context, next) => AdminEndpointConventions.RequireAnyPermission(context, next, Backend.Security.AdminPermissionCatalog.SeasonReadPermissionKeys))
            .WithName("GetAdminSeasonDetail")
            .WithOpenApi();
        group.MapPut("/seasons/{seasonId:int}", UpdateSeason)
            .AddEndpointFilter((context, next) => AdminEndpointConventions.RequireWritePermission(context, next, Backend.Security.AdminPermissionCatalog.Years))
            .WithName("UpdateAdminSeason")
            .WithOpenApi();
        group.MapDelete("/seasons/{seasonId:int}", DeleteSeason)
            .AddEndpointFilter((context, next) => AdminEndpointConventions.RequireWritePermission(context, next, Backend.Security.AdminPermissionCatalog.Years))
            .WithName("DeleteAdminSeason")
            .WithOpenApi();
        group.MapPost("/seasons/{seasonId:int}/categories", CreateCategory)
            .AddEndpointFilter((context, next) => AdminEndpointConventions.RequireWritePermission(context, next, Backend.Security.AdminPermissionCatalog.Categories))
            .WithName("CreateAdminCategory")
            .WithOpenApi();
        group.MapPut("/categories/{categoryId:int}", UpdateCategory)
            .AddEndpointFilter((context, next) => AdminEndpointConventions.RequireWritePermission(context, next, Backend.Security.AdminPermissionCatalog.Categories))
            .WithName("UpdateAdminCategory")
            .WithOpenApi();
        group.MapDelete("/categories/{categoryId:int}", DeleteCategory)
            .AddEndpointFilter((context, next) => AdminEndpointConventions.RequireWritePermission(context, next, Backend.Security.AdminPermissionCatalog.Categories))
            .WithName("DeleteAdminCategory")
            .WithOpenApi();
        group.MapPut("/seasons/{seasonId:int}/subcategory-templates", UpdateSeasonSubcategoryTemplates)
            .AddEndpointFilter((context, next) => AdminEndpointConventions.RequireWritePermission(context, next, Backend.Security.AdminPermissionCatalog.Categories))
            .WithName("UpdateAdminSeasonSubcategoryTemplates")
            .WithOpenApi();
        group.MapPost("/seasons/{seasonId:int}/category-groups", CreateCategoryGroup)
            .AddEndpointFilter((context, next) => AdminEndpointConventions.RequireWritePermission(context, next, Backend.Security.AdminPermissionCatalog.Categories))
            .WithName("CreateAdminCategoryGroup")
            .WithOpenApi();
        group.MapPut("/seasons/{seasonId:int}/category-groups/{groupName}", UpdateCategoryGroup)
            .AddEndpointFilter((context, next) => AdminEndpointConventions.RequireWritePermission(context, next, Backend.Security.AdminPermissionCatalog.Categories))
            .WithName("UpdateAdminCategoryGroup")
            .WithOpenApi();
        group.MapDelete("/seasons/{seasonId:int}/category-groups/{groupName}", DeleteCategoryGroup)
            .AddEndpointFilter((context, next) => AdminEndpointConventions.RequireWritePermission(context, next, Backend.Security.AdminPermissionCatalog.Categories))
            .WithName("DeleteAdminCategoryGroup")
            .WithOpenApi();
        group.MapPost("/seasons/{seasonId:int}/candidates", CreateCandidate)
            .AddEndpointFilter((context, next) => AdminEndpointConventions.RequireWritePermission(context, next, Backend.Security.AdminPermissionCatalog.Candidates))
            .WithName("CreateAdminCandidate")
            .WithOpenApi();
        group.MapPut("/candidates/{candidateId:int}", UpdateCandidate)
            .AddEndpointFilter((context, next) => AdminEndpointConventions.RequireWritePermission(context, next, Backend.Security.AdminPermissionCatalog.Candidates))
            .WithName("UpdateAdminCandidate")
            .WithOpenApi();
        group.MapGet("/candidates/{candidateId:int}/delete-preview", GetCandidateDeletePreview)
            .AddEndpointFilter((context, next) => AdminEndpointConventions.RequireWritePermission(context, next, Backend.Security.AdminPermissionCatalog.Candidates))
            .WithName("GetAdminCandidateDeletePreview")
            .WithOpenApi();
        group.MapDelete("/candidates/{candidateId:int}", DeleteCandidate)
            .AddEndpointFilter((context, next) => AdminEndpointConventions.RequireWritePermission(context, next, Backend.Security.AdminPermissionCatalog.Candidates))
            .WithName("DeleteAdminCandidate")
            .WithOpenApi();
        group.MapPost("/seasons/{seasonId:int}/results", SetResult)
            .AddEndpointFilter((context, next) => AdminEndpointConventions.RequireWritePermission(context, next, Backend.Security.AdminPermissionCatalog.Winners))
            .WithName("SetAdminResult")
            .WithOpenApi();
        group.MapDelete("/results/{resultId:int}", DeleteResult)
            .AddEndpointFilter((context, next) => AdminEndpointConventions.RequireWritePermission(context, next, Backend.Security.AdminPermissionCatalog.Winners))
            .WithName("DeleteAdminResult")
            .WithOpenApi();
        group.MapPost("/seasons/{seasonId:int}/winners/publish", PublishWinners)
            .AddEndpointFilter((context, next) => AdminEndpointConventions.RequireWritePermission(context, next, Backend.Security.AdminPermissionCatalog.Winners))
            .WithName("PublishAdminSeasonWinners")
            .WithOpenApi();
        group.MapPost("/seasons/{seasonId:int}/winners/unpublish", UnpublishWinners)
            .AddEndpointFilter((context, next) => AdminEndpointConventions.RequireWritePermission(context, next, Backend.Security.AdminPermissionCatalog.Winners))
            .WithName("UnpublishAdminSeasonWinners")
            .WithOpenApi();
        group.MapGet("/seasons/{seasonId:int}/workflow-rules", GetWorkflowRules)
            .AddEndpointFilter((context, next) => AdminEndpointConventions.RequirePermission(context, next, Backend.Security.AdminPermissionCatalog.Settings))
            .WithName("GetAdminWorkflowRules")
            .WithOpenApi();
        group.MapPut("/seasons/{seasonId:int}/workflow-rules", UpdateWorkflowRules)
            .AddEndpointFilter((context, next) => AdminEndpointConventions.RequireWritePermission(context, next, Backend.Security.AdminPermissionCatalog.Settings))
            .WithName("UpdateAdminWorkflowRules")
            .WithOpenApi();
        return group;
    }
}
