<?php
// Permite acesso de aplicativos externos (CORS)
ini_set('display_errors', 0);
error_reporting(0);
header('Access-Control-Allow-Origin: *');

require_once 'config.php';
$pdo = getConexaoAtiva();
$pdo_int = getConexaoInterna();
$pdo->exec("SET NAMES utf8mb4");

// ====================================================================
// INTELIGÊNCIA ARTIFICIAL: DETECTA O SISTEMA (XUI / XTREAM)
// ====================================================================
$modo_ativo = 'interno';
try { $modo_ativo = $pdo_int->query("SELECT modo_ativo FROM configuracoes LIMIT 1")->fetchColumn() ?: 'interno'; } catch (Exception $e) {}

$tabela_clientes = ($modo_ativo === 'xui') ? '`lines`' : '`users`';
$tabela_categorias = 'stream_categories';
$tabela_series = 'series';
$tabela_episodios = 'series_episodes';
$col_sort = 'sort';

if ($modo_ativo === 'xui') {
    $col_sort = 'episode_num';
    try { $pdo->query("SELECT 1 FROM streams_categories LIMIT 1"); $tabela_categorias = 'streams_categories'; } catch (Exception $e) {}
    try { $pdo->query("SELECT 1 FROM streams_series LIMIT 1"); $tabela_series = 'streams_series'; } catch (Exception $e) {}
    try { $pdo->query("SELECT 1 FROM streams_episodes LIMIT 1"); $tabela_episodios = 'streams_episodes'; } catch (Exception $e) {}
}

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

if (empty($username) || empty($password)) {
    header('Content-Type: application/json; charset=utf-8');
    die(json_encode(['user_info' => ['auth' => 0]]));
}

$stmtUser = $pdo->prepare("SELECT * FROM $tabela_clientes WHERE username = ? AND password = ? LIMIT 1");
$stmtUser->execute([$username, $password]);
$user = $stmtUser->fetch(PDO::FETCH_ASSOC);

if (!$user || $user['enabled'] == 0) {
    header('Content-Type: application/json; charset=utf-8');
    die(json_encode(['user_info' => ['auth' => 0]]));
}

$status = 'Active';
if ($user['exp_date'] != 0 && $user['exp_date'] != null && $user['exp_date'] < time()) {
    $status = 'Expired';
    if (empty($action)) {
        header('Content-Type: application/json; charset=utf-8');
        die(json_encode(['user_info' => ['auth' => 0, 'status' => 'Expired']]));
    } else {
        http_response_code(403); die("Conta vencida.");
    }
}
try { $pdo->prepare("UPDATE $tabela_clientes SET last_activity = ? WHERE id = ?")->execute([time(), $user['id']]); } catch (Exception $e){}

function extrair_id_categoria($raw_cat) {
    if (empty($raw_cat)) return "0";
    if (strpos($raw_cat, '[') !== false) {
        $arr = json_decode($raw_cat, true);
        if (is_array($arr) && isset($arr[0])) return (string)$arr[0];
        preg_match('/\d+/', $raw_cat, $matches);
        return $matches[0] ?? "0";
    }
    return (string)$raw_cat;
}

// ==============================================================================
// ROTEAMENTO: API JSON (XCIPTV, SMARTERS, 9XTREAM)
// ==============================================================================
header('Content-Type: application/json; charset=utf-8');

switch ($action) {

    case 'get_live_categories':
    case 'get_vod_categories':
    case 'get_series_categories':
        $mapeamento_categorias = [];
        if ($action == 'get_live_categories') $stmtMap = $pdo->query("SELECT DISTINCT category_id FROM streams WHERE type IN (1, 3, 4)");
        elseif ($action == 'get_vod_categories') $stmtMap = $pdo->query("SELECT DISTINCT category_id FROM streams WHERE type = 2");
        else $stmtMap = $pdo->query("SELECT DISTINCT category_id FROM $tabela_series");
        
        while ($row = $stmtMap->fetch(PDO::FETCH_ASSOC)) {
            $cat_limpo = extrair_id_categoria($row['category_id']);
            if ($cat_limpo != "0") $mapeamento_categorias[$cat_limpo] = true;
        }

        $todas_cats = [];
        if ($action == 'get_series_categories') {
            try { $todas_cats = $pdo->query("SELECT * FROM series_categories")->fetchAll(PDO::FETCH_ASSOC); } catch(Exception $e) {}
        }
        if (empty($todas_cats)) {
            try { $todas_cats = $pdo->query("SELECT * FROM $tabela_categorias")->fetchAll(PDO::FETCH_ASSOC); } catch(Exception $e) {}
        }
        
        $categorias_finais = [];
        foreach ($todas_cats as $c) {
            $id_str = (string)($c['id'] ?? '0');
            if (isset($mapeamento_categorias[$id_str])) {
                $categorias_finais[] = [
                    "category_id" => $id_str,
                    "category_name" => str_replace('"', '', $c['category_name'] ?? 'Sem Nome'),
                    "parent_id" => isset($c['parent_id']) ? (int)$c['parent_id'] : 0
                ];
            }
        }
        echo json_encode($categorias_finais);
        break;

    case 'get_live_streams':
        $cat_id = $_GET['category_id'] ?? null;
        $stmt = $pdo->query("SELECT * FROM streams WHERE type IN (1, 3, 4)");
        $streams = $stmt->fetchAll(PDO::FETCH_ASSOC);

        $resultado = [];
        $num = 1;
        foreach ($streams as $s) {
            $cat_real = extrair_id_categoria($s['category_id'] ?? "0");
            if ($cat_id && $cat_real !== (string)$cat_id) continue;

            $resultado[] = [
                "num" => $num++, "name" => str_replace('"', '', $s['stream_display_name'] ?? 'Canal'),
                "stream_type" => "live", "stream_id" => (int)$s['id'],
                "stream_icon" => $s['stream_icon'] ?? '', "epg_channel_id" => $s['custom_sid'] ?? "", 
                "added" => $s['added'] ?? "0", "category_id" => $cat_real,
                "custom_sid" => $s['custom_sid'] ?? "", "tv_archive" => 0, "direct_source" => "", "tv_archive_duration" => 0
            ];
        }
        echo json_encode($resultado);
        break;

    case 'get_vod_streams':
        $cat_id = $_GET['category_id'] ?? null;
        $stmt = $pdo->query("SELECT * FROM streams WHERE type = 2");
        $streams = $stmt->fetchAll(PDO::FETCH_ASSOC);

        $resultado = [];
        $num = 1;
        foreach ($streams as $s) {
            $cat_real = extrair_id_categoria($s['category_id'] ?? "0");
            if ($cat_id && $cat_real !== (string)$cat_id) continue;

            $ext = isset($s['target_container']) ? trim(str_replace(['[', ']', '"'], '', $s['target_container'])) : "mp4";
            if (empty($ext)) $ext = "mp4";

            // RASTREADOR DE SINOPSE
            $plot = "";
            if (!empty($s['plot'])) $plot = $s['plot'];
            elseif (!empty($s['overview'])) $plot = $s['overview'];
            elseif (!empty($s['notes'])) $plot = $s['notes'];
            if (empty(trim($plot))) $plot = "Sinopse não disponível.";

            $resultado[] = [
                "num" => $num++, "name" => str_replace('"', '', $s['stream_display_name'] ?? 'Filme'),
                "stream_type" => "movie", "stream_id" => (int)$s['id'], "stream_icon" => $s['stream_icon'] ?? '',
                "rating" => "5", "rating_5based" => "2.5", "added" => $s['added'] ?? "0",
                "category_id" => $cat_real, "container_extension" => $ext, "custom_sid" => null,
                "direct_source" => "", "plot" => $plot
            ];
        }
        echo json_encode($resultado);
        break;

    case 'get_vod_info':
        $vod_id = $_GET['vod_id'] ?? 0;
        $stmtVod = $pdo->prepare("SELECT * FROM streams WHERE id = ? AND type = 2");
        $stmtVod->execute([$vod_id]);
        $info = $stmtVod->fetch(PDO::FETCH_ASSOC);

        if (!$info) { echo json_encode([]); break; }

        $cover = $info['stream_icon'] ?? '';
        $name = str_replace('"', '', $info['stream_display_name'] ?? 'Filme');
        $ext = trim(str_replace(['[', ']', '"'], '', $info['target_container'] ?? 'mp4')) ?: "mp4";
        $cat_real = extrair_id_categoria($info['category_id'] ?? "0");

        // RASTREADOR DE SINOPSE
        $plot = "";
        if (!empty($info['plot'])) $plot = $info['plot'];
        elseif (!empty($info['overview'])) $plot = $info['overview'];
        elseif (!empty($info['notes'])) $plot = $info['notes'];
        if (empty(trim($plot))) $plot = "Sinopse não disponível.";

        $resultado = [
            "info" => [
                "name" => $name, "o_name" => $name, "cover_big" => $cover, "movie_image" => $cover, 
                "description" => $plot, "plot" => $plot, "genre" => "Diversos", "backdrop_path" => [$cover], "rating" => "5"
            ],
            "movie_data" => [
                "stream_id" => (int)$vod_id, "name" => $name, "title" => $name,
                "added" => $info['added'] ?? "0", "category_id" => $cat_real,
                "container_extension" => $ext, "custom_sid" => "", "direct_source" => ""
            ]
        ];
        echo json_encode($resultado);
        break;

    case 'get_series':
        $cat_id = $_GET['category_id'] ?? null;
        $stmt = $pdo->query("SELECT * FROM $tabela_series");
        $series = $stmt->fetchAll(PDO::FETCH_ASSOC);

        $resultado = [];
        $num = 1;
        foreach ($series as $s) {
            $cat_real = extrair_id_categoria($s['category_id'] ?? "0");
            if ($cat_id && $cat_real !== (string)$cat_id) continue;

            $plot = "";
            if (!empty($s['plot'])) $plot = $s['plot'];
            elseif (!empty($s['overview'])) $plot = $s['overview'];
            if (empty(trim($plot))) $plot = "Sinopse não disponível.";

            $resultado[] = [
                "num" => $num++, "name" => str_replace('"', '', $s['title'] ?? 'Serie'),
                "series_id" => (int)$s['id'], "cover" => $s['cover'] ?? '',
                "plot" => $plot, "cast" => "", "director" => "", "genre" => "",
                "releaseDate" => "", "last_modified" => $s['last_modified'] ?? "0",
                "rating" => "5", "rating_5based" => "2.5", 
                "backdrop_path" => [$s['cover'] ?? ''], 
                "youtube_trailer" => "", "episode_run_time" => "0", "category_id" => $cat_real
            ];
        }
        echo json_encode($resultado);
        break;

    case 'get_series_info':
        $series_id = $_GET['series_id'] ?? 0;
        $stmtInfo = $pdo->prepare("SELECT * FROM $tabela_series WHERE id = ?");
        $stmtInfo->execute([$series_id]);
        $info = $stmtInfo->fetch(PDO::FETCH_ASSOC);

        if (!$info) { echo json_encode([]); break; }

        $stmtEps = $pdo->prepare("SELECT se.*, s.* FROM $tabela_episodios se LEFT JOIN streams s ON se.stream_id = s.id WHERE se.series_id = ? ORDER BY se.season_num ASC, se.{$col_sort} ASC");
        $stmtEps->execute([$series_id]);
        $episodes_raw = $stmtEps->fetchAll(PDO::FETCH_ASSOC);

        $episodes_formatted = [];
        $seasons_array = [];
        $seasons_tracker = [];

        foreach ($episodes_raw as $ep) {
            $s_num = (int)($ep['season_num'] ?? 1);
            if (!isset($episodes_formatted[$s_num])) { $episodes_formatted[$s_num] = []; }
            if (!in_array($s_num, $seasons_tracker)) {
                $seasons_tracker[] = $s_num;
                $seasons_array[] = ["season_number" => $s_num];
            }

            $ext = trim(str_replace(['[', ']', '"'], '', $ep['target_container'] ?? 'mp4')) ?: "mp4";

            $episodes_formatted[$s_num][] = [
                "id" => (string)$ep['stream_id'],
                "episode_num" => (string)($ep[$col_sort] ?? "1"),
                "title" => str_replace('"', '', $ep['stream_display_name'] ?? 'Episodio'),
                "container_extension" => $ext,
                "info" => ["plot" => "", "duration" => "", "movie_image" => "", "bitrate" => ""],
                "custom_sid" => null, "added" => $ep['added'] ?? "0", "season" => $s_num, "direct_source" => ""
            ];
        }

        $plot = "";
        if (!empty($info['plot'])) $plot = $info['plot'];
        elseif (!empty($info['overview'])) $plot = $info['overview'];
        if (empty(trim($plot))) $plot = "Sinopse não disponível.";

        $cat_real = extrair_id_categoria($info['category_id'] ?? "0");
        $resultado = [
            "seasons" => $seasons_array,
            "info" => [
                "name" => str_replace('"', '', $info['title'] ?? 'Serie'),
                "cover" => $info['cover'] ?? '',
                "plot" => $plot,
                "last_modified" => $info['last_modified'] ?? "0",
                "rating" => "5", "rating_5based" => "2.5",
                "category_id" => $cat_real,
                "backdrop_path" => [$info['cover'] ?? ''], 
                "youtube_trailer" => "", "episode_run_time" => "0"
            ],
            "episodes" => (object)$episodes_formatted 
        ];
        echo json_encode($resultado);
        break;

    default:
        $server_info = [
            "url" => $_SERVER['HTTP_HOST'], "port" => $_SERVER['SERVER_PORT'] ?? "80",
            "https_port" => "443", "server_protocol" => isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http",
            "rtmp_port" => "25462", "timezone" => "America/Sao_Paulo", "timestamp_now" => time(), "time_now" => date('Y-m-d H:i:s')
        ];

        $telas_ativas = (!empty($user['last_activity']) && (time() - (int)$user['last_activity']) <= 60) ? 1 : 0;
        $user_info = [
            "username" => $user['username'], "password" => $user['password'], "message" => "Bem Vindo", "auth" => 1, "status" => $status,
            "exp_date" => (string)$user['exp_date'], "is_trial" => "0", "active_cons" => (string)$telas_ativas, "created_at" => (string)time(),
            "max_connections" => (string)($user['max_connections'] ?? "1"), "allowed_output_formats" => ["m3u8", "ts"]
        ];

        echo json_encode(["user_info" => $user_info, "server_info" => $server_info]);
        break;
}
?>