<?php

namespace App\PaymentGateways\OfflinePayments;

use AllowDynamicProperties;
use App\Contracts\PaymentGatewayContract;
use App\Models\Order;
use App\Services\OrderService;

#[AllowDynamicProperties]
class OfflinePaymentsGateway implements PaymentGatewayContract
{
    public function __construct(protected OrderService $orderService)
    {
    }

    public function isConfigured(): bool
    {
        return setting('offline_payments.enabled') == 'yes' &&
            !empty(setting('offline_payments.title')) &&
            !empty(setting('offline_payments.description'));
    }

    public function renderForm(): array
    {
        return [
            'id' => $this->getIdentifier(),
            'description' => setting('offline_payments.description'),
        ];
    }

    public function processPayment(Order $order)
    {
        $this->orderService->update($order, 'payment_method', 'offline_payments');
        $this->orderService->update($order, 'offline_payment_method', setting('offline_payments.title'));
        $this->orderService->handlePostPaymentFlow($order, setting('offline_payments.order_status', getConstant('ORDER_STATUS_PROCESSING')));

        return response()->json(['redirect_url' => route('ch_order_submitted', [$order->encrypted_id])]);
        //->redirect();
    }

    public function completePurchase(Order $order): void
    {

    }

    public function processIPN()
    {
        // TODO: Implement processIPN() method.
    }

    public function getToken()
    {
        // TODO: Implement getToken() method.
    }

    public function getLabel(): string
    {
        return setting('offline_payments.title', 'Offline Payments');
    }

    public function getIdentifier(): string
    {
        return 'offline_payments';
    }
}
