<?php

namespace App\Http\Controllers\Api\Customer;

use App\Http\Controllers\Controller;
use App\Models\Order;
use App\Services\OnlinePaymentService;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Auth;

class PaymentGatewayController extends Controller
{
    public function __construct(private OnlinePaymentService $gateway) {}

    /**
     * POST /api/customer/payment/{order}/initiate
     * Starts an online payment for an unpaid order and returns the URL the app
     * should open so the customer can pay on the gateway.
     */
    public function initiate(Order $order): JsonResponse
    {
        abort_unless($order->user_id === Auth::guard('customer')->id(), 403, 'This order is not yours.');

        $data = $this->gateway->initiate($order, (string) $order->payment_method);

        return response()->json($data);
    }
}
