<?php
header('Content-Type: application/json');
require_once __DIR__ . '/../config.php';
require_once __DIR__ . '/../db.php';
require_once __DIR__ . '/../session.php';
require_once __DIR__ . '/../is_admin.php';

$action = $_GET['action'] ?? '';

if ($action === 'get_servers') {
    $servers_raw = $pdo->query("SELECT id, name, ip_address, ssh_user, ssh_port, tags, traffic_limit_gb, expiration_date FROM servers ORDER BY name ASC")->fetchAll(PDO::FETCH_ASSOC);
    
    $servers = array_map(function($server) {
        $server['expiration_date_jalali'] = $server['expiration_date'] ? to_jalali($server['expiration_date']) : 'نامحدود';
        return $server;
    }, $servers_raw);

    echo json_encode($servers);
    exit;
}

echo json_encode(['error' => 'Invalid action']);
?>