using Indotalent.ConfigBackEnd.Extensions;
using Indotalent.Features.Sales.SalesQuotation.Cqrs;
using MediatR;
using Microsoft.AspNetCore.Authentication.JwtBearer;

namespace Indotalent.Features.Sales.SalesQuotation;

public static class SalesQuotationEndpoint
{
    public static void MapSalesQuotationEndpoints(this IEndpointRouteBuilder app)
    {
        var group = app.MapGroup("/sales-quotation").WithTags("SalesQuotations")
            .RequireAuthorization(policy => policy.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
            .RequireAuthenticatedUser());

        group.MapGet("/", async (IMediator mediator) =>
        {
            var result = await mediator.Send(new GetSalesQuotationListQuery());
            return result.ToApiResponse("Sales quotation list retrieved successfully");
        })
        .WithName("GetSalesQuotationList");

        group.MapGet("/{id}", async (string id, IMediator mediator) =>
        {
            var result = await mediator.Send(new GetSalesQuotationByIdQuery(id));
            return result.ToApiResponse(result is not null
                ? "Sales quotation detail retrieved successfully"
                : $"Sales quotation with ID {id} not found");
        })
        .WithName("GetSalesQuotationById");

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

        group.MapPost("/", async (CreateSalesQuotationRequest request, IMediator mediator) =>
        {
            var result = await mediator.Send(new CreateSalesQuotationCommand(request));
            return result.ToApiResponse("Sales quotation has been created successfully", StatusCodes.Status201Created);
        })
        .WithName("CreateSalesQuotation");

        group.MapPost("/update", async (UpdateSalesQuotationRequest request, IMediator mediator) =>
        {
            var result = await mediator.Send(new UpdateSalesQuotationCommand(request));
            if (!result)
            {
                return ((object?)null).ToApiResponse("Update failed. Sales quotation not found.");
            }
            return result.ToApiResponse("Sales quotation has been updated successfully");
        })
        .WithName("UpdateSalesQuotation");

        group.MapPost("/delete/{id}", async (string id, IMediator mediator) =>
        {
            var result = await mediator.Send(new DeleteSalesQuotationByIdCommand(id));
            if (!result)
            {
                return ((object?)null).ToApiResponse("Delete failed. Sales quotation not found.");
            }
            return true.ToApiResponse("Sales quotation has been deleted successfully");
        })
        .WithName("DeleteSalesQuotationById");

        group.MapPost("/sales-quotation-item", async (CreateSalesQuotationItemRequest request, IMediator mediator) =>
        {
            var result = await mediator.Send(new CreateSalesQuotationItemCommand(request));
            return result.ToApiResponse("Item has been added successfully");
        })
        .WithName("CreateSalesQuotationItem");

        group.MapPost("/sales-quotation-item/update", async (UpdateSalesQuotationItemRequest request, IMediator mediator) =>
        {
            var result = await mediator.Send(new UpdateSalesQuotationItemCommand(request));
            if (!result)
            {
                return ((object?)null).ToApiResponse("Update item failed.");
            }
            return result.ToApiResponse("Item has been updated successfully");
        })
        .WithName("UpdateSalesQuotationItem");

        group.MapPost("/sales-quotation-item/delete/{id}", async (string id, IMediator mediator) =>
        {
            var result = await mediator.Send(new DeleteSalesQuotationItemCommand(id));
            if (!result)
            {
                return ((object?)null).ToApiResponse("Delete item failed.");
            }
            return true.ToApiResponse("Item has been removed successfully");
        })
        .WithName("DeleteSalesQuotationItem");
    }
}