<?php

require __DIR__ . '/vendor/autoload.php';

$app = require_once __DIR__ . '/bootstrap/app.php';
$app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();

function printResult(string $title, array $result): void
{
    echo "=============================\n";
    echo "TEST: {$title}\n";
    echo "=============================\n";
    echo json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) . "\n\n";
}

function parseArgs(array $argv): array
{
    $params = [];
    foreach ($argv as $arg) {
        if (strpos($arg, '=') !== false) {
            [$key, $value] = explode('=', $arg, 2);
            $params[$key] = $value;
        }
    }
    return $params;
}

$service = new App\Services\RMTOService();
$action = $argv[1] ?? 'help';
$params = parseArgs(array_slice($argv, 2));

switch ($action) {
    case 'current-date':
        printResult('GetRmtoCurrentDate', $service->getRmtoCurrentDate());
        break;

    case 'company-status':
        printResult('GetCompanyStatus', $service->getCompanyStatus());
        break;

    case 'real-person':
        $nationalCode = $params['national_code'] ?? ($params['n'] ?? '1234567890');
        printResult('Inquiry_RealPerson', $service->inquiryRealPerson($nationalCode));
        break;

    case 'postal-code':
        $postalCode = $params['postal_code'] ?? ($params['p'] ?? '1234567890');
        printResult('Inquiry_PostalCode', $service->inquiryPostalCode($postalCode));
        break;

    case 'price':
        $defaults = [
            'origin_code' => '1',
            'dest_code' => '2',
            'bargir_code' => '5',
            'kala_code' => '1',
            'weight' => '300',
            'company_commission' => '50',
            'is_traffic_load' => 'false',
        ];

        $payload = array_merge($defaults, $params);
        $payload['is_traffic_load'] = in_array(strtolower($payload['is_traffic_load']), ['1', 'true', 'yes'], true);

        printResult('Inquiry_OrfOstanPrice', $service->inquiryOrfOstanPrice($payload));
        break;

    case 'all':
        printResult('GetRmtoCurrentDate', $service->getRmtoCurrentDate());
        printResult('GetCompanyStatus', $service->getCompanyStatus());
        printResult('Inquiry_RealPerson', $service->inquiryRealPerson($params['national_code'] ?? '1234567890'));
        printResult('Inquiry_PostalCode', $service->inquiryPostalCode($params['postal_code'] ?? '1234567890'));
        $priceParams = [
            'origin_code' => $params['origin_code'] ?? '1',
            'dest_code' => $params['dest_code'] ?? '2',
            'bargir_code' => $params['bargir_code'] ?? '5',
            'kala_code' => $params['kala_code'] ?? '1',
            'weight' => $params['weight'] ?? '300',
            'company_commission' => $params['company_commission'] ?? '50',
            'is_traffic_load' => in_array(strtolower($params['is_traffic_load'] ?? 'false'), ['1', 'true', 'yes'], true),
        ];
        printResult('Inquiry_OrfOstanPrice', $service->inquiryOrfOstanPrice($priceParams));
        break;

    case 'help':
    default:
        echo "RMTO Direct Test Script\n";
        echo "Usage:\n";
        echo "  php rmto_direct_test.php current-date\n";
        echo "  php rmto_direct_test.php company-status\n";
        echo "  php rmto_direct_test.php real-person national_code=1234567890\n";
        echo "  php rmto_direct_test.php postal-code postal_code=1234567890\n";
        echo "  php rmto_direct_test.php price origin_code=1 dest_code=2 bargir_code=5 kala_code=1 weight=300 company_commission=50 is_traffic_load=false\n";
        echo "  php rmto_direct_test.php all\n";
        echo "\n";
        echo "Note: this script runs outside the admin panel and uses the same RMTO service config.\n";
        break;
}
