<?php

namespace App\Http\Controllers;

use App\Models\PaymentRequest;
use App\Models\PaymentTransaction;
use App\Services\Payments\BanglaQrPaymentService;
use Illuminate\Http\Request;

class BanglaQrPaymentController extends Controller
{
    public function __construct(private readonly BanglaQrPaymentService $banglaQrPaymentService)
    {
    }

    public function pay(Request $request)
    {
        $paymentRequest = PaymentRequest::findOrFail($request->payment_id);
        $transaction = $this->banglaQrPaymentService->initiatePaymentRequest($paymentRequest);

        return view('payment.bangla-qr', [
            'paymentRequest' => $paymentRequest,
            'transaction' => $transaction,
            'callback' => $paymentRequest->external_redirect_link,
        ]);
    }

    public function callback(Request $request)
    {
        $transaction = $this->banglaQrPaymentService->handleCallback($request);

        return response()->json([
            'status' => $transaction->status,
            'transaction_id' => $transaction->transaction_id,
            'verified_by_system' => (bool) $transaction->verified_by_system,
        ]);
    }

    public function status(PaymentTransaction $paymentTransaction)
    {
        return response()->json($this->banglaQrPaymentService->status($paymentTransaction));
    }
}
