<?php

declare(strict_types=1);

require_once __DIR__ . '/bootstrap.php';
require_admin();

$pageTitle = $pageTitle ?? 'Painel Profissional';
$pageSubtitle = $pageSubtitle ?? 'Administração completa do aplicativo xciptv.';
$pageIcon = $pageIcon ?? 'fa-tv';
$currentFile = basename((string)($_SERVER['SCRIPT_NAME'] ?? 'index.php'));
$flashMessage = pull_flash();

if (!function_exists('max_nav_active')) {
    function max_nav_active(string $file, string $currentFile): string
    {
        return $file === $currentFile ? ' active' : '';
    }
}

$navItems = [
    ['dashboard.php', 'fa-chart-line', 'Dashboard'],
    ['dns.php', 'fa-server', 'Conexões DNS'],
    ['app.php', 'fa-mobile-screen-button', 'Aplicativo e API'],
    ['buttons.php', 'fa-table-cells-large', 'Botões do App'],
    ['branding.php', 'fa-image', 'Logotipo do App'],
    ['interface.php', 'fa-sliders', 'Interface'],
    ['players.php', 'fa-circle-play', 'Players'],
    ['messages.php', 'fa-bullhorn', 'Mensagens'],
    ['maintenance.php', 'fa-screwdriver-wrench', 'Manutenção'],
    ['devices.php', 'fa-tv', 'Dispositivos'],
    ['user.php', 'fa-user-shield', 'Meu Perfil'],
];
?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no, viewport-fit=cover">
    <meta name="author" content="Max Rebrands Aplicativos">
    <meta name="theme-color" content="#08080c">
    <title><?=e($pageTitle)?> | Max Rebrands</title>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&family=Orbitron:wght@600;700;800&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.2.3/animate.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.2/css/all.min.css">
    <link href="<?=e(panel_url('css/simple-sidebar.css'))?>" rel="stylesheet">
    <link href="<?=e(panel_url('css/max-rebrands-panel.css'))?>?v=20260617_mobile_fix_v3" rel="stylesheet">
    <link href="<?=e(panel_url('css/omnimax-integration.css'))?>?v=20260617_mobile_fix_v3" rel="stylesheet">
</head>
<body>
<div id="js-particles"></div>
<div class="d-flex" id="wrapper">
    <aside id="sidebar-wrapper" class="max-sidebar" aria-label="Menu principal">
        <div class="sidebar-brand-block">
            <div class="brand-orbit">
                <img src="<?=e(panel_url('img/max_rebrands_logo.png'))?>" alt="Max Rebrands" class="sidebar-logo">
            </div>
            <div class="brand-title">MAX REBRANDS</div>
            <div class="brand-subtitle">Xciptv Panel</div>
        </div>

        <div class="list-group list-group-flush max-menu">
            <?php foreach ($navItems as [$file, $icon, $label]): ?>
                <a class="list-group-item list-group-item-action<?=max_nav_active($file, $currentFile)?>" href="<?=e(panel_url($file))?>">
                    <i class="fa-solid <?=e($icon)?>"></i><span><?=e($label)?></span>
                </a>
            <?php endforeach; ?>
        </div>
    </aside>

    <main id="page-content-wrapper" class="max-content">
        <nav class="navbar navbar-expand-lg navbar-dark max-topbar">
            <button class="btn max-menu-toggle" id="menu-toggle" type="button" aria-label="Abrir menu" aria-controls="sidebar-wrapper" aria-expanded="false">
                <i class="fa-solid fa-bars"></i>
            </button>
            <div class="topbar-title">
                <img src="<?=e(panel_url('img/max_rebrands_logo.png'))?>" alt="Max Rebrands" class="topbar-logo">
                <div>
                    <strong>Max Rebrands</strong>
                    <small>Administração de streaming</small>
                </div>
            </div>
            <div class="center" id="pageMessages"></div>
            <a href="<?=e(panel_url('logout.php'))?>" class="btn max-logout ml-auto"><i class="fa-solid fa-right-from-bracket"></i> <span>Sair</span></a>
        </nav>

        <div class="container-fluid max-container">
            <section class="panel-hero animated fadeIn">
                <div class="hero-icon"><i class="fa-solid <?=e($pageIcon)?>"></i></div>
                <div class="hero-copy">
                    <h1><?=e($pageTitle)?></h1>
                    <p><?=e($pageSubtitle)?></p>
                </div>
                <div class="hero-badge"><i class="fa-solid fa-play-circle"></i> Streaming Pro</div>
            </section>

            <?php if ($flashMessage):
                $flashType = (string)($flashMessage['type'] ?? 'info');
                $bootstrapType = $flashType === 'error' ? 'danger' : ($flashType === 'success' ? 'success' : 'info');
            ?>
                <div class="alert alert-<?=e($bootstrapType)?> max-flash-alert" role="alert">
                    <i class="fa-solid <?=$bootstrapType === 'danger' ? 'fa-triangle-exclamation' : 'fa-circle-check'?>"></i>
                    <span><?=e($flashMessage['message'] ?? '')?></span>
                </div>
            <?php endif; ?>
