<?php
if (session_status() === PHP_SESSION_NONE) {
    @session_start();
}

$dial = $_GET['dial'] ?? '';
$code = $_GET['code'] ?? '';

if (!empty($dial) && !empty($code)) {
    // Basic sanitization
    $dial = preg_replace('/[^0-9+]/', '', $dial);
    $code = strtoupper(preg_replace('/[^A-Za-z]/', '', $code));
    
    // Map code/dial to currency
    $currency = 'USD';
    if ($code === 'IN' || $dial === '+91') {
        $currency = 'INR';
    } elseif ($code === 'PK' || $dial === '+92') {
        $currency = 'PKR';
    }
    
    $_SESSION['user_country_code'] = $dial;
    $_SESSION['user_currency'] = $currency;
    
    header('Content-Type: application/json');
    echo json_encode([
        'status' => 'success',
        'dial' => $dial,
        'code' => $code,
        'currency' => $currency
    ]);
    exit();
}

header('Content-Type: application/json');
echo json_encode(['status' => 'error', 'message' => 'Invalid parameters']);
