<?php

namespace App\Http\Controllers\Shop;

use App\Http\Controllers\Controller;
use App\Repositories\Core\User\ShopCart;
use App\Repositories\Shop\Paybox as PayboxGateway;
use App\Repositories\Shop\Contents;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;


class PayboxController extends Controller
{
    public function accepted()
    {
        ShopCart::clear();

        return view('paybox.paybox', ['content' => Contents::getPayboxConfirmedContent()]);
    }

    public function refused(Request $request)
    {
        return view('paybox.paybox', ['content' => Contents::getPayboxRefusedContent()]);
    }

    public function aborted()
    {
        return view('paybox.paybox', ['content' => Contents::getPayboxAbortedContent()]);
    }

    public function waiting()
    {
        return view('paybox.waiting', ['content' => Contents::getPayboxWaitingContent()]);
    }

    public function process(Request $request)
    {
        $invoiceId = $request->input('order_number');

        if (! $invoiceId) {
            Log::warning('Paybox callback missing order_number', ['payload' => $request->all()]);

            return response('Missing order_number', 400);
        }

        $success = PayboxGateway::verifyPayment($invoiceId);

        if (! $success) {
            return response('KO', 400);
        }

        return response('OK');
    }
}
