<?php
declare(strict_types=1);

error_reporting(0);
require_once __DIR__ . '/includes/Database.php';

$pdo = Database::getInstance();

// Помощна функция за настройки
function getSetting($pdo, $name, $default = '') {
    $stmt = $pdo->prepare("SELECT value FROM settings WHERE name = ?");
    $stmt->execute([$name]);
    return $stmt->fetchColumn() ?: $default;
}

// Проверка за info_hash
if (!isset($_GET['info_hash'])) {
    sendError(100, 'Missing info_hash parameter');
}

// Обработка на info_hash (суров 20-байтов низ)
$infoHashes = [];
if (is_array($_GET['info_hash'])) {
    foreach ($_GET['info_hash'] as $hash) {
        if (is_string($hash) && strlen($hash) === 20) {
            $infoHashes[] = bin2hex($hash);
        }
    }
} else {
    if (is_string($_GET['info_hash']) && strlen($_GET['info_hash']) === 20) {
        $infoHashes[] = bin2hex($_GET['info_hash']);
    }
}

if (empty($infoHashes) || count($infoHashes) > 74) {
    sendError(101, 'Invalid or too many info_hash values (max 74 allowed)');
}

// === 1. Вземи статистика от peers за всички info_hash ===
$placeholders = str_repeat('?,', count($infoHashes) - 1) . '?';
$stmt = $pdo->prepare("
    SELECT 
        info_hash,
        SUM(seeder = 1) AS complete,
        SUM(seeder = 0) AS incomplete
    FROM peers 
    WHERE info_hash IN ($placeholders) 
      AND last_announce >= NOW() - INTERVAL 30 MINUTE
    GROUP BY info_hash
");
$stmt->execute($infoHashes);
$peerStats = [];
foreach ($stmt->fetchAll() as $row) {
    $peerStats[$row['info_hash']] = [
        'complete' => (int)$row['complete'],
        'incomplete' => (int)$row['incomplete']
    ];
}

// === 2. Вземи downloaded (completed) само за вътрешните торенти ===
$downloadedMap = [];
$stmt = $pdo->prepare("
    SELECT info_hash, completed 
    FROM torrents 
    WHERE info_hash IN ($placeholders)
");
$stmt->execute($infoHashes);
foreach ($stmt->fetchAll() as $row) {
    $downloadedMap[$row['info_hash']] = (int)$row['completed'];
}

// === 3. Формирай отговора ===
$response = ['files' => []];
foreach ($infoHashes as $hash) {
    $stats = $peerStats[$hash] ?? ['complete' => 0, 'incomplete' => 0];
    $downloaded = $downloadedMap[$hash] ?? 0;

    $response['files'][$hash] = [
        'complete'   => $stats['complete'],
        'incomplete' => $stats['incomplete'],
        'downloaded' => $downloaded
    ];
}

// Изпрати bencode отговор
header('Content-Type: text/plain');
echo bencode($response);
exit;

// --- Helper функции ---
function bencode($data) {
    if (is_array($data)) {
        if (isset($data[0]) || empty($data)) { // list
            $encoded = 'l';
            foreach ($data as $item) {
                $encoded .= bencode($item);
            }
            return $encoded . 'e';
        } else { // dict
            ksort($data, SORT_STRING);
            $encoded = 'd';
            foreach ($data as $key => $value) {
                $encoded .= bencode((string)$key) . bencode($value);
            }
            return $encoded . 'e';
        }
    }
    if (is_int($data)) {
        return 'i' . $data . 'e';
    }
    if (is_string($data)) {
        return strlen($data) . ':' . $data;
    }
    return '0:';
}

function sendError(int $code, string $message) {
    header('Content-Type: text/plain');
    echo bencode(['failure reason' => $message, 'error code' => $code]);
    exit;
}