<?php

namespace App\Http\Controllers;

use App\Services\Payments\PayPalService;
use App\Services\Payments\StripeService;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Log;

class WebhookController extends Controller
{
    public function __construct(
        protected StripeService $stripeService,
        protected PayPalService $paypalService
    ) {}

    /**
     * Handle Stripe webhook.
     */
    public function stripe(Request $request): Response
    {
        $payload = $request->getContent();
        $signature = $request->header('Stripe-Signature');

        Log::info('Stripe webhook received', [
            'signature_present' => ! empty($signature),
        ]);

        $result = $this->stripeService->handleWebhook($payload, $signature);

        Log::info('Stripe webhook processed', $result);

        return response($result['status'] === 'error' ? 'Error' : 'OK', 200);
    }

    /**
     * Handle PayPal webhook.
     */
    public function paypal(Request $request): Response
    {
        $payload = $request->getContent();
        $headers = $request->headers->all();

        Log::info('PayPal webhook received');

        $result = $this->paypalService->handleWebhook($headers, $payload);

        Log::info('PayPal webhook processed', $result);

        return response($result['status'] === 'error' ? 'Error' : 'OK', 200);
    }
}
