<?php

namespace Licenzo\Http\Controller\Api\V1;

use Licenzo\Framework\Http\Request;
use Licenzo\Http\Traits\HandlesErrors;
use Licenzo\Services\Integrations\StripeWebhookService;

// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

class StripeWebhookController
{
    use HandlesErrors;

    /**
     * @var StripeWebhookService
     */
    private $service;

    public function __construct(?StripeWebhookService $service = null)
    {
        $this->service = $service ?? new StripeWebhookService();
    }

    /**
     * Public Stripe webhook endpoint.
     *
     * @return array<string,mixed>
     */
    public function handle(Request $request): array
    {
        try {
            return $this->service->handle($request);
        } catch (\Throwable $e) {
            return $this->handleError($e, 'Stripe webhook processing failed');
        }
    }
}
