<?php
define('SECURE_ACCESS', true);
$_SERVER['REQUEST_METHOD'] = 'POST';

require_once __DIR__ . '/../includes/config.php';
require_once __DIR__ . '/../includes/db.php';
require_once __DIR__ . '/../includes/auth.php';

if (session_status() === PHP_SESSION_NONE) {
    session_start();
}
$_SESSION['user_id'] = 1;
$_SESSION['username'] = 'admin';
$_SESSION['role'] = 'admin';
$_SESSION['csrf_token'] = 'dummy_token';

$mode = $argv[1] ?? '';

if ($mode === 'short_prefix') {
    $_POST = [
        'csrf_token' => 'dummy_token',
        'save_general' => '1',
        'emp_code_prefix' => 'EP',
        'emp_code_length' => '15'
    ];
} elseif ($mode === 'short_length') {
    $_POST = [
        'csrf_token' => 'dummy_token',
        'save_general' => '1',
        'emp_code_prefix' => 'EPPP',
        'emp_code_length' => '10'
    ];
} elseif ($mode === 'valid_inputs') {
    $_POST = [
        'csrf_token' => 'dummy_token',
        'save_general' => '1',
        'emp_code_prefix' => 'EPPP',
        'emp_code_length' => '15',
        'site_name' => 'RENENET EDUCARE',
        'site_tagline' => 'Events & MLMs',
        'site_logo' => 'assets/img/logo.png',
        'site_favicon' => 'assets/img/favicon.ico',
        'registration_mode' => 'invite_only',
        'default_referrer_code' => 'REPLUS123456789',
        'company_email' => 'admin@reneneteducare.com',
        'company_address' => 'Bandra Kurla Complex, Mumbai, Maharashtra - 400051',
        'company_gstin' => '27AAACE1234F1Z0',
        'gst_rate' => '18.00',
        'default_referral_commission' => '10.00',
        'signup_bonus' => '100.00',
        'referral_bonus' => '50.00',
        'referral_bonus_levels' => '3',
        'referral_bonus_cap' => '500.00',
        'emp_code_default_type' => 'generated',
        'emp_code_custom_value' => '',
        'razorpay_key_id' => 'key_123',
        'razorpay_key_secret' => 'secret_123',
        'bank_verification_min_deposit' => '0.01',
        'bank_verification_max_deposit' => '0.99',
        'default_kyc_required_docs' => '2',
        'footer_copyright_text' => 'All rights reserved.',
        'footer_about_text' => 'An amazing platform',
        'footer_facebook_url' => '',
        'footer_twitter_url' => '',
        'footer_linkedin_url' => '',
        'footer_instagram_url' => ''
    ];
}

ob_start();
include __DIR__ . '/../api/admin_settings.php';
$output = ob_get_clean();

$json_start = strpos($output, '{');
if ($json_start !== false) {
    $output = substr($output, $json_start);
}

$data = json_decode($output, true);
if ($mode === 'short_prefix') {
    if (!$data['success'] && strpos($data['message'], "prefix must be exactly 4 or 5 characters") !== false) {
        echo "PASSED";
    } else {
        echo "FAILED: " . ($data['message'] ?? 'no message');
    }
} elseif ($mode === 'short_length') {
    if (!$data['success'] && strpos($data['message'], "length must be between 15 and 30 characters") !== false) {
        echo "PASSED";
    } else {
        echo "FAILED: " . ($data['message'] ?? 'no message');
    }
} elseif ($mode === 'valid_inputs') {
    if ($data['success']) {
        echo "PASSED";
    } else {
        echo "FAILED: " . ($data['message'] ?? 'no message');
    }
}
