<?php
include 'config.php';

$keywords = explode(PHP_EOL, file_get_contents($config['keywords']));
$url = $config['url'];

$td_pairs = array();
if (file_exists($config['td'])) {
    $td_content = file_get_contents($config['td']);
    $td_lines = explode(PHP_EOL, $td_content);
    
    foreach ($td_lines as $line) {
        $line = trim($line);
        if (empty($line)) continue;
        
        $parts = explode('|||', $line);
        if (count($parts) >= 2) {
            $td_pairs[] = array(
                'title'       => trim($parts[0]),
                'description' => trim($parts[1]),
                'p1'          => isset($parts[2]) ? trim($parts[2]) : '',
                'p2'          => isset($parts[3]) ? trim($parts[3]) : '',
                'p3'          => isset($parts[4]) ? trim($parts[4]) : ''
            );
        }
    }
}

if(!file_exists($config['sitemap'])) {
    $doc = new DOMDocument('1.0', 'UTF-8');
    $doc->formatOutput = true;

    $urlset = $doc->createElement('urlset');
    $urlset->setAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9');
    $doc->appendChild($urlset);

    foreach ($keywords as $keyword) {
        $kwUrl = $url . '?' . $config['parameter'] . '=' . urlencode(trim($keyword));
        
        $urlElement = $doc->createElement('url');
        $locElement = $doc->createElement('loc', htmlspecialchars($kwUrl));
        $urlElement->appendChild($locElement);
        $lastmodElement = $doc->createElement('lastmod', date("Y-m-d"));
        $urlElement->appendChild($lastmodElement);
        $changefreqElement = $doc->createElement('changefreq', 'daily');
        $urlElement->appendChild($changefreqElement);
        $urlset->appendChild($urlElement);
    }

    $doc->save($config['sitemap']);
}

$brand = null;
if (isset($_GET[$config['parameter']]) && !empty($_GET[$config['parameter']])) {
    $paramValue = trim($_GET[$config['parameter']]);
    foreach ($keywords as $keyword) {
        if (strtolower($paramValue) == strtolower(trim($keyword))) {
            $brand = trim($keyword);
            break;
        }
    }
}

if ($brand !== null) {
    $html = file_get_contents($config['template']);
    $html = str_replace('{{ u }}', rtrim($url, '/'), $html);
    $html = str_replace('{{ BRAND }}', $brand, $html);

    $brandUrl = $url . '?' . $config['parameter'] . '=' . urlencode($brand);
    $html = str_replace('{{ URL }}', $brandUrl, $html);

    $title = "{{ BRAND }} — Jangan Asal Main Sebelum Pahami Karakter Tiap Provider";
    $description = "Setiap provider punya karakter berbeda — Pragmatic, PG Soft, Habanero, semuanya beda. {{ BRAND }} kupas perbedaan volatilitas, RTP, dan pola gameplay biar kamu nggak salah pilih. Pelajari sebelum spin.";
    $p1 = '';
    $p2 = '';
    $p3 = '';

    if (!empty($td_pairs)) {
        $brandIndex = (int) array_search(strtolower($brand), array_map('strtolower', array_map('trim', $keywords)));
        $td_pair = $td_pairs[$brandIndex % count($td_pairs)];
        $title       = $td_pair['title'];
        $description = $td_pair['description'];
        $p1 = $td_pair['p1'];
        $p2 = $td_pair['p2'];
        $p3 = $td_pair['p3'];

        $p1 = str_replace('{{ u }}', rtrim($url, '/'), $p1);
        $p2 = str_replace('{{ u }}', rtrim($url, '/'), $p2);
        $p3 = str_replace('{{ u }}', rtrim($url, '/'), $p3);

        $title       = str_replace(['{{ BRAND }}', '{ BRAND }'], $brand, $title);
        $description = str_replace(['{{ BRAND }}', '{ BRAND }'], $brand, $description);
        $p1 = str_replace(['{{ BRAND }}', '{ BRAND }'], $brand, $p1);
        $p2 = str_replace(['{{ BRAND }}', '{ BRAND }'], $brand, $p2);
        $p3 = str_replace(['{{ BRAND }}', '{ BRAND }'], $brand, $p3);
    }

    $html = str_replace('{{ t }}', $title, $html);
    $html = str_replace('{{ d }}', $description, $html);
    $html = str_replace('{{ p1 }}', $p1, $html);
    $html = str_replace('{{ p2 }}', $p2, $html);
    $html = str_replace('{{ p3 }}', $p3, $html);

    echo $html;
    exit;
}
header("HTTP/1.1 404 Not Found");
echo "<h1>IZIN YA KOKO</h1>";
exit;
