<?php

namespace App\Services\Payments\Gateways;

use App\Models\UserPaymentMethod;
use App\Services\Payments\Contracts\PaymentGateway;
use App\Services\Payments\GatewayResult;
use Illuminate\Support\Str;

class SimulatedPaymentGateway implements PaymentGateway
{
    public function supports(string $provider): bool
    {
        return true;
    }

    public function charge(
        UserPaymentMethod $method,
        float $amount,
        string $currency,
        array $metadata = [],
    ): GatewayResult {
        return new GatewayResult(
            status: GatewayResult::STATUS_PAID,
            providerReference: 'sim_ch_' . Str::lower(Str::random(16)),
            payload: [
                'simulated' => true,
                'provider' => $method->provider,
                'amount' => $amount,
                'currency' => strtoupper($currency),
                'metadata' => $metadata,
            ],
        );
    }

    public function payout(
        UserPaymentMethod $method,
        float $amount,
        string $currency,
        array $metadata = [],
    ): GatewayResult {
        $status = $method->provider === 'rib'
            ? GatewayResult::STATUS_PENDING
            : GatewayResult::STATUS_PAID;

        return new GatewayResult(
            status: $status,
            providerReference: 'sim_po_' . Str::lower(Str::random(16)),
            payload: [
                'simulated' => true,
                'provider' => $method->provider,
                'amount' => $amount,
                'currency' => strtoupper($currency),
                'metadata' => $metadata,
            ],
        );
    }
}
