<?php
session_start();
require_once '../includes/db.php';
header('Content-Type: application/json');

if (!isset($_SESSION['user_id'])) { echo json_encode(['status'=>false, 'msg'=>'Login expirado']); exit; }

$action = $_POST['action'] ?? $_GET['action'] ?? '';

// --- GERENCIAR CANAIS ---

if ($action == 'save_channel') {
    $id = $_POST['id'];
    $name = $_POST['name'];
    $url = $_POST['stream_url'];
    $logo = $_POST['logo_url'];
    $cat = (int)$_POST['category_id'];

    try {
        if(empty($id)) {
            // Criar Novo
            $pdo->prepare("INSERT INTO channels (name, stream_url, logo_url, category_id) VALUES (?, ?, ?, ?)")->execute([$name, $url, $logo, $cat]);
            echo json_encode(['status'=>true, 'msg'=>'Canal criado!']);
        } else {
            // Editar Existente
            $pdo->prepare("UPDATE channels SET name=?, stream_url=?, logo_url=?, category_id=? WHERE id=?")->execute([$name, $url, $logo, $cat, $id]);
            echo json_encode(['status'=>true, 'msg'=>'Canal atualizado!']);
        }
    } catch(Exception $e) { echo json_encode(['status'=>false, 'msg'=>'Erro ao salvar.']); }
}

if ($action == 'delete_channel') {
    $id = $_POST['id'];
    $pdo->prepare("DELETE FROM channels WHERE id=?")->execute([$id]);
    echo json_encode(['status'=>true, 'msg'=>'Canal deletado!']);
}

if ($action == 'get_channel') {
    $stmt = $pdo->prepare("SELECT * FROM channels WHERE id=?");
    $stmt->execute([$_GET['id']]);
    echo json_encode(['status'=>true, 'data'=>$stmt->fetch(PDO::FETCH_ASSOC)]);
}

// --- GERENCIAR FILMES (Lógica idêntica a canais, mudando a tabela) ---

if ($action == 'save_movie') {
    $id = $_POST['id']; $name = $_POST['name']; $url = $_POST['stream_url']; $cover = $_POST['cover_url']; $desc = $_POST['description']; $cat = (int)$_POST['category_id'];
    try {
        if(empty($id)) {
            $pdo->prepare("INSERT INTO movies (name, stream_url, cover_url, description, category_id) VALUES (?, ?, ?, ?, ?)")->execute([$name, $url, $cover, $desc, $cat]);
        } else {
            $pdo->prepare("UPDATE movies SET name=?, stream_url=?, cover_url=?, description=?, category_id=? WHERE id=?")->execute([$name, $url, $cover, $desc, $cat, $id]);
        }
        echo json_encode(['status'=>true, 'msg'=>'Filme salvo!']);
    } catch(Exception $e) { echo json_encode(['status'=>false, 'msg'=>'Erro ao salvar.']); }
}

if ($action == 'delete_movie') {
    $pdo->prepare("DELETE FROM movies WHERE id=?")->execute([$_POST['id']]);
    echo json_encode(['status'=>true, 'msg'=>'Filme deletado!']);
}

if ($action == 'get_movie') {
    $stmt = $pdo->prepare("SELECT * FROM movies WHERE id=?"); $stmt->execute([$_GET['id']]);
    echo json_encode(['status'=>true, 'data'=>$stmt->fetch(PDO::FETCH_ASSOC)]);
}

// --- GERENCIAR CATEGORIAS (Simples) ---
if ($action == 'save_category') {
    $name = $_POST['name']; $type = $_POST['type']; // channel, movie, series
    $pdo->prepare("INSERT INTO categories (name, type) VALUES (?, ?)")->execute([$name, $type]);
    echo json_encode(['status'=>true, 'msg'=>'Categoria criada!']);
}
?>