<?php

declare(strict_types=1);

include_once __DIR__ . '/../login.php';

function rtSendSecurityHeaders(string $nonce = ''): void
{
    if (headers_sent()) {
        return;
    }

    header('Content-Type: application/json; charset=UTF-8');
    header('X-Content-Type-Options: nosniff');
    header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');
    header('Pragma: no-cache');
    header('Expires: 0');
}

function rtOutputJson(array $payload): void
{
    $json = json_encode(
        $payload,
        JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE,
    );

    if ($json === false) {
        echo '{"data":[]}';

        return;
    }

    echo $json;
}

function rtNormalizeUtf8(string $value): string
{
    if ($value === '') {
        return '';
    }

    if (preg_match('//u', $value) === 1) {
        return $value;
    }

    if (function_exists('mb_convert_encoding')) {
        $converted = mb_convert_encoding($value, 'UTF-8', 'UTF-8, ISO-8859-1, Windows-1252');

        if (is_string($converted) && preg_match('//u', $converted) === 1) {
            return $converted;
        }
    }

    if (function_exists('iconv')) {
        $converted = iconv('Windows-1252', 'UTF-8//IGNORE', $value);

        if (is_string($converted) && preg_match('//u', $converted) === 1) {
            return $converted;
        }

        $converted = iconv('ISO-8859-1', 'UTF-8//IGNORE', $value);

        if (is_string($converted) && preg_match('//u', $converted) === 1) {
            return $converted;
        }
    }

    return '';
}

function rtEscapeHtml(string $value): string
{
    return htmlspecialchars(rtNormalizeUtf8($value), ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
}

function rtSvgImg(string $src, string $title = '', int $size = 14): string
{
    return '<img src="' . rtEscapeHtml(statAssetUrl($src)) . '" width="' . $size . '" height="' . $size . '" class="rt-icon" alt="" title="' . rtEscapeHtml($title) . '">';
}

function rtStringLength(string $value): int
{
    $normalized = rtNormalizeUtf8($value);

    if (function_exists('mb_strlen')) {
        return mb_strlen($normalized, 'UTF-8');
    }

    return strlen($normalized);
}

rtSendSecurityHeaders();

try {
    /** @var PDO $pdo */
    $pdo = require __DIR__ . '/../../connection_pdo.php';

    if (!isset($_GET['date']) || !is_string($_GET['date'])) {
        rtOutputJson(['data' => []]);
        exit;
    }

    $dateRaw = $_GET['date'];
    $dateParam = $dateRaw === '2' ? 'yesterday' : 'today';
    $conversionDate = date('Y-m-d', strtotime($dateParam));

    $topClickId = null;

    try {
        $sqlTop = '
            SELECT click_id, SUM(payout) AS total_payout
            FROM leadreport
            WHERE conversion_date = :date
            GROUP BY click_id
            ORDER BY total_payout DESC
            LIMIT 1
        ';

        $stmtTop = $pdo->prepare($sqlTop);
        $stmtTop->execute(['date' => $conversionDate]);
        $rowTop = $stmtTop->fetch(PDO::FETCH_ASSOC);

        if ($rowTop !== false && $rowTop !== null) {
            $topClickId = (string) ($rowTop['click_id'] ?? '');
        }
    } catch (Throwable $e) {
        // Silently proceed without top click ID
    }

    $sql = '
        SELECT
            id,
            click_id,
            ip_address,
            country,
            traffic,
            currency_symbol,
            payout,
            conversion_date,
            network
        FROM leadreport
        WHERE conversion_date = :date
        ORDER BY id ASC
    ';

    $stmt = $pdo->prepare($sql);
    $stmt->execute(['date' => $conversionDate]);

    $response = [
        'data' => [],
    ];

    $number = 1;

    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        $clickIdRaw = rtNormalizeUtf8((string) ($row['click_id'] ?? ''));
        $networkRaw = strtoupper(rtNormalizeUtf8((string) ($row['network'] ?? '')));
        $trafficRaw = strtoupper(rtNormalizeUtf8((string) ($row['traffic'] ?? '')));
        $countryRaw = strtoupper(rtNormalizeUtf8((string) ($row['country'] ?? '')));

        $idClickDisplay = rtSvgImg('/dist/user.svg', 'User') . ' ' . rtEscapeHtml($clickIdRaw);

        if ($topClickId !== null && $clickIdRaw === $topClickId) {
            $idClickDisplay = rtSvgImg('/dist/top.svg', 'Top Earner') . ' ' . rtEscapeHtml($clickIdRaw);
        }

        if ($clickIdRaw === 'SCATTER') {
            $idClickDisplay = rtSvgImg('/dist/scatter.svg', 'Scatter')
                . ' <span class="neons"><em>-' . rtEscapeHtml($clickIdRaw) . '-</em></span>';
        }

        if ($clickIdRaw === '') {
            $idClickDisplay = rtSvgImg('/dist/ngix.svg', 'NGIX') . ' NGIX';
        }

        if (rtStringLength($clickIdRaw) > 20) {
            $idClickDisplay = rtSvgImg('/dist/user.svg', 'User') . ' YELLOW';
        }

        $trafficSvg = [
            'WAP' => '/dist/wap.svg',
            'WEB' => '/dist/web.svg',
            'TABLET' => '/dist/tablet.svg',
        ];

        if (isset($trafficSvg[$trafficRaw])) {
            $trafficDisplay = rtSvgImg($trafficSvg[$trafficRaw], $trafficRaw);
        } else {
            $trafficDisplay = '<code class="rt-code">' . rtEscapeHtml($trafficRaw) . '</code>';
        }

        $networkSvg = [
            'LOSPOLLOS' => '/dist/lp.svg',
            'IMONETIZEIT' => '/dist/imo.svg',
            'IMONETIZEIT-1' => '/dist/imo.svg',
            'IMONETIZEIT-2' => '/dist/imo.svg',
            'IMONETIZEIT2' => '/dist/imo.svg',
            'TRAFEE' => '/dist/tf.svg',
            'TORAZZO' => '/dist/custom.svg',
            'CUSTOM' => '/dist/custom.svg',
            'OFFER' => '/dist/custom.svg',
        ];

        if (isset($networkSvg[$networkRaw])) {
            $networkDisplay = rtSvgImg($networkSvg[$networkRaw], $networkRaw, 16);
        } elseif ($networkRaw === '') {
            $networkDisplay = rtSvgImg('/dist/imo.svg', 'iMonetizeIt', 16);
        } elseif ($networkRaw === 'ADVERTEN') {
            $networkDisplay = '<span class="label rt-label-adverten">ADVERTEN</span>';
        } else {
            $networkDisplay = rtEscapeHtml($networkRaw);
        }

        $ipAddress = rtNormalizeUtf8((string) ($row['ip_address'] ?? ''));
        $countryClass = strtolower($countryRaw);

        if (preg_match('/^[a-z]{2}$/', $countryClass) !== 1) {
            $countryClass = 'xx';
        }

        $ipDisplay = '<a href="#" class="js-ip rt-ip" id="' . rtEscapeHtml($ipAddress) . '" data-ip="'
            . rtEscapeHtml($ipAddress) . '"><span> ' . rtEscapeHtml($ipAddress) . '</span></a>';

        $countryDisplay = '<span class="flag flag-' . rtEscapeHtml($countryClass) . '"></span> '
            . rtEscapeHtml($countryRaw);

        $currencySymbol = rtNormalizeUtf8((string) ($row['currency_symbol'] ?? '$'));
        $payoutValue = (float) ($row['payout'] ?? 0.0);
        $payoutDisplay = rtEscapeHtml($currencySymbol) . number_format($payoutValue, 2);

        $conversionDateValue = rtNormalizeUtf8((string) ($row['conversion_date'] ?? ''));
        $conversionDateDisplay = '<span class="text-muted">' . rtEscapeHtml($conversionDateValue) . '</span>';

        $response['data'][] = [
            'id' => $number++,
            'click_id' => $idClickDisplay,
            'ip_address' => $ipDisplay,
            'country' => $countryDisplay,
            'traffic' => $trafficDisplay,
            'payout' => $payoutDisplay,
            'conversion_date' => $conversionDateDisplay,
            'network' => $networkDisplay,
        ];
    }

    rtOutputJson($response);
} catch (Throwable $e) {
    error_log('[realtime/data] ' . $e->getMessage());
    rtOutputJson(['data' => []]);
}
