<?php
// Minimal Xtream-compatible get.php wrapper for TMDB-to-VOD installs that do not ship one.
require_once __DIR__ . '/config.php';
$username = isset($_GET['username']) ? $_GET['username'] : '';
$password = isset($_GET['password']) ? $_GET['password'] : '';
$output = isset($_GET['output']) ? strtolower($_GET['output']) : 'm3u8';
$mode = isset($GLOBALS['spandamanSplitMode']) ? strtolower((string)$GLOBALS['spandamanSplitMode']) : '';

function spandaman_abs_url($path) {
    if (preg_match('#^https?://#i', (string)$path)) return $path;
    $scheme = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
    return $scheme . '://' . $_SERVER['HTTP_HOST'] . rtrim(dirname($_SERVER['SCRIPT_NAME']), '/') . '/' . ltrim((string)$path, '/');
}

function spandaman_json_to_m3u($jsonFile, $kind) {
    if (!file_exists(__DIR__ . '/' . $jsonFile) || filesize(__DIR__ . '/' . $jsonFile) <= 8) return false;
    $rows = json_decode(file_get_contents(__DIR__ . '/' . $jsonFile), true);
    if (!is_array($rows)) return false;
    header('Content-Type: audio/x-mpegurl; charset=utf-8');
    echo "#EXTM3U\n";
    foreach ($rows as $row) {
        if (!is_array($row)) continue;
        $name = $row['name'] ?? $row['title'] ?? ('SPANDAMAN ' . strtoupper($kind));
        $logo = $row['stream_icon'] ?? $row['cover'] ?? '';
        $group = $row['group'] ?? ($kind === 'adult' ? 'Adult Movies' : ($kind === 'series' ? 'Series' : 'Live'));
        $id = $row['stream_id'] ?? $row['series_id'] ?? '';
        $url = $row['direct_source'] ?? '';
        if (!$url && $kind === 'series' && $id !== '') $url = spandaman_abs_url('series/' . rawurlencode($_GET['username'] ?? 'series') . '/' . rawurlencode($_GET['password'] ?? 'series') . '/' . $id . '.mp4');
        if (!$url && $id !== '') $url = spandaman_abs_url('play.php?movieId=' . rawurlencode((string)$id));
        $url = str_replace('[[SERVER_URL]]', spandaman_abs_url(''), (string)$url);
        if (!$url) continue;
        echo '#EXTINF:-1 group-title="' . str_replace('"', "'", $group) . '" tvg-id="' . str_replace('"', "'", $name) . '" tvg-logo="' . str_replace('"', "'", $logo) . '",' . $name . "\n";
        echo $url . "\n";
    }
    return true;
}

if ($mode === 'adult' && spandaman_json_to_m3u('adult-movies.json', 'adult')) exit;
if ($mode === 'series') {
    header('Content-Type: audio/x-mpegurl; charset=utf-8');
    echo "#EXTM3U\n# SPANDAMAN SERIES: use Xtream player_api get_series and get_series_info for episode playback.\n";
    exit;
}

$candidates = ($output === 'ts')
    ? ['channels/live_playlist_verified.m3u8', 'spandaman_playlist_ts.m3u', 'spandaman_playlist.m3u', 'playlist.m3u8', 'movies.m3u8', 'tv.m3u8', 'channels/live_playlist.m3u8']
    : ['channels/live_playlist_verified.m3u8', 'channels/live_playlist.m3u8', 'spandaman_playlist.m3u8', 'playlist.m3u8', 'movies.m3u8', 'tv.m3u8'];
$file = null;
foreach ($candidates as $candidate) {
    if (file_exists(__DIR__ . '/' . $candidate) && filesize(__DIR__ . '/' . $candidate) > 8) {
        $file = $candidate;
        break;
    }
}
if (!$file) {
    echo "#EXTM3U\n# SPANDAMAN split install: no playlist generated yet. Run updatebatch.php?dev=true first.\n";
    exit;
}
header('Content-Type: audio/x-mpegurl; charset=utf-8');
$body = file_get_contents(__DIR__ . '/' . $file);
$base = spandaman_abs_url('');
$body = str_replace(
    [
        'https://apps.spandaman.co.uk/spandaman/current/',
        'http://apps.spandaman.co.uk/spandaman/current/',
    ],
    $base,
    $body
);
echo $body;
?>
