using Indotalent.ConfigBackEnd.Extensions;
using Indotalent.Features.Pipeline.Campaign.Cqrs;
using MediatR;
using Microsoft.AspNetCore.Authentication.JwtBearer;

namespace Indotalent.Features.Pipeline.Campaign;

public static class CampaignEndpoint
{
    public static void MapCampaignEndpoints(this IEndpointRouteBuilder app)
    {
        var group = app.MapGroup("/campaign").WithTags("Campaign")
            .RequireAuthorization(policy => policy.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
            .RequireAuthenticatedUser());

        group.MapGet("/lookup", async (IMediator mediator) =>
        {
            var result = await mediator.Send(new GetCampaignLookupQuery());
            return result.ToApiResponse("Lookup data retrieved successfully");
        })
        .WithName("GetCampaignLookup");

        group.MapGet("/", async (IMediator mediator) =>
        {
            var result = await mediator.Send(new GetCampaignListQuery());
            return result.ToApiResponse("Campaign list retrieved successfully");
        })
        .WithName("GetCampaignList");

        group.MapGet("/{id}", async (string id, IMediator mediator) =>
        {
            var result = await mediator.Send(new GetCampaignByIdQuery(id));
            return result.ToApiResponse(result is not null
                ? "Campaign detail retrieved successfully"
                : "Campaign not found");
        })
        .WithName("GetCampaignById");

        group.MapPost("/", async (CreateCampaignRequest request, IMediator mediator) =>
        {
            var result = await mediator.Send(new CreateCampaignCommand(request));
            return result.ToApiResponse("Campaign created successfully", StatusCodes.Status201Created);
        })
        .WithName("CreateCampaign");

        group.MapPost("/update", async (UpdateCampaignRequest request, IMediator mediator) =>
        {
            var result = await mediator.Send(new UpdateCampaignCommand(request));
            return result.ToApiResponse("Campaign updated successfully");
        })
        .WithName("UpdateCampaign");

        group.MapPost("/delete/{id}", async (string id, IMediator mediator) =>
        {
            var result = await mediator.Send(new DeleteCampaignByIdCommand(id));
            return result.ToApiResponse("Campaign deleted successfully");
        })
        .WithName("DeleteCampaign");

        group.MapPost("/budget", async (CreateBudgetRequest request, IMediator mediator) =>
        {
            var result = await mediator.Send(new CreateBudgetCommand(request));
            return result.ToApiResponse("Budget added successfully");
        })
        .WithName("AddCampaignBudget");

        group.MapPost("/budget/update", async (UpdateBudgetRequest request, IMediator mediator) =>
        {
            var result = await mediator.Send(new UpdateBudgetCommand(request));
            return result.ToApiResponse("Budget updated successfully");
        })
        .WithName("UpdateCampaignBudget");

        group.MapPost("/budget/delete/{id}", async (string id, IMediator mediator) =>
        {
            var result = await mediator.Send(new DeleteBudgetCommand(id));
            return result.ToApiResponse("Budget deleted successfully");
        })
        .WithName("DeleteCampaignBudget");

        group.MapPost("/expense", async (CreateExpenseRequest request, IMediator mediator) =>
        {
            var result = await mediator.Send(new CreateExpenseCommand(request));
            return result.ToApiResponse("Expense added successfully");
        })
        .WithName("AddCampaignExpense");

        group.MapPost("/expense/update", async (UpdateExpenseRequest request, IMediator mediator) =>
        {
            var result = await mediator.Send(new UpdateExpenseCommand(request));
            return result.ToApiResponse("Expense updated successfully");
        })
        .WithName("UpdateCampaignExpense");

        group.MapPost("/expense/delete/{id}", async (string id, IMediator mediator) =>
        {
            var result = await mediator.Send(new DeleteExpenseCommand(id));
            return result.ToApiResponse("Expense deleted successfully");
        })
        .WithName("DeleteCampaignExpense");
    }
}