import { Body, Controller, Post, Route, SuccessResponse } from "tsoa";
import { MercadoPagoWebhookService } from "./MercadoPagoWebhook.Service";
import { ApiValidationError } from "../../Models/Server.Error.model";
import { ApiVoidResult } from "../../Models/Api.VoidResult.type";

@Route("mercadopago")
export class MercadoPagoWebhookController extends Controller {
    @SuccessResponse(200, "Done")
    @Post("webhook")
    public async handleWebhook(
        @Body() requestBody: any
    ): Promise<ApiVoidResult | ApiValidationError> {
        try {
            await new MercadoPagoWebhookService().handleWebhook(requestBody);
            this.setStatus(200);
            return {
                success: true,
                message: "Webhook processed",
            };
        } catch (e) {
            const errorOccurred: Error = e as Error;
            this.setStatus(500);
            return new ApiValidationError(500, errorOccurred.message);
        }
    }
}
