<?php
require_once __DIR__ . "/../MaintenanceCheck.php";
session_start();

require "../Auth/db.php";

$username = $_SESSION["user"]["username"] ?? "Guest";

$avatar = "../Assets/Logo.png";

if (
    !empty($_SESSION["user"]["avatar"]) &&
    !empty($_SESSION["user"]["discord_id"])
) {
    $avatar = "https://cdn.discordapp.com/avatars/" .
        $_SESSION["user"]["discord_id"] . "/" .
        $_SESSION["user"]["avatar"] . ".png";
}

$selectedCategory = $_GET["category"] ?? "Most Popular";

if ($selectedCategory === "Most Popular") {

    $stmt = $pdo->query("
        SELECT *
        FROM media_series
        ORDER BY id DESC
    ");

} elseif ($selectedCategory === "Most Rating") {

    $stmt = $pdo->query("
        SELECT *
        FROM media_series
        ORDER BY CAST(rating AS DECIMAL(3,1)) DESC
    ");

} elseif ($selectedCategory === "Most Recent") {

    $stmt = $pdo->query("
        SELECT *
        FROM media_series
        ORDER BY id DESC
    ");

} else {

    $stmt = $pdo->prepare("
        SELECT *
        FROM media_series
        WHERE category = ?
        ORDER BY id DESC
    ");

    $stmt->execute([$selectedCategory]);
}

$series = $stmt->fetchAll(PDO::FETCH_ASSOC);

if (!empty($_SESSION["user"]["id"]) && !empty($seriesId)) {
    $historyStmt = $pdo->prepare("
        INSERT INTO user_history (user_id, movie_id, type, watched_at)
        VALUES (?, ?, 'series', NOW())
        ON DUPLICATE KEY UPDATE watched_at = NOW()
    ");

    $historyStmt->execute([
        $_SESSION["user"]["id"],
        $seriesId
    ]);
}
?>
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<title>TV Shows - Rewire</title>

<link rel="icon" type="image/png" href="../Assets/Logo.png">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<script src="https://unpkg.com/lucide@latest"></script>

<style>

*{
    margin:0;
    padding:0;
    box-sizing:border-box;
    font-family:Arial,sans-serif;
}

body{
    background:#050505;
    color:white;
}

/* HEADER */

.header{
    position:fixed;
    top:0;
    left:0;
    width:100%;
    height:82px;
    background:rgba(5,5,5,0.96);
    backdrop-filter:blur(12px);
    border-bottom:1px solid rgba(255,255,255,0.06);
    z-index:999;

    display:flex;
    align-items:center;
    justify-content:space-between;

    padding:0 25px;
}

.logo-img{
    height:70px;
}

.right{
    display:flex;
    align-items:center;
    gap:30px;
}

.nav{
    display:flex;
    align-items:center;
    gap:25px;
}

.home-btn,
.browse-btn,
.search-icon-btn{
    background:none;
    border:none;
    color:#ccc;
    cursor:pointer;
    display:flex;
    align-items:center;
    justify-content:center;
    text-decoration:none;
    transition:0.25s ease;
}

.home-btn:hover,
.browse-btn:hover,
.search-icon-btn:hover{
    color:#00c2ff;
    transform:scale(1.08);
}

.home-btn i,
.search-icon-btn i,
.search-icon-btn svg{
    width:22px;
    height:22px;
}

.browse-btn{
    gap:6px;
}

.browse-btn i{
    width:21px;
    height:21px;
}

.browse-arrow{
    width:16px !important;
    height:16px !important;
}

.browse-dropdown{
    position:relative;
    display:flex;
    align-items:center;
}

.browse-menu{
    position:absolute;
    top:34px;
    left:50%;
    transform:translateX(-50%);
    display:none;
    gap:8px;
    background:rgba(8,10,13,0.97);
    border:1px solid rgba(255,255,255,0.14);
    border-radius:14px;
    padding:9px;
    box-shadow:0 12px 35px rgba(0,0,0,0.55);
    z-index:999;
}

.browse-menu.active{
    display:flex;
}

.browse-menu a{
    width:38px;
    height:38px;
    border-radius:10px;
    color:#c8d0d8;
    display:flex;
    align-items:center;
    justify-content:center;
    text-decoration:none;
}

.browse-menu a:hover{
    background:rgba(0,194,255,0.12);
    color:#00c2ff;
}

.browse-menu i{
    width:20px;
    height:20px;
}

.profile-dropdown{
    position:relative;
    cursor:pointer;
}

.profile-avatar{
    width:36px;
    height:36px;
    border-radius:10px;
    object-fit:cover;
    border:1px solid rgba(255,255,255,0.25);
}

.profile-menu {
    position: absolute;
    top: 55px;
    right: 0;
    width: 185px;
    background: rgba(8,10,13,0.97);
    border: 1px solid rgba(255,255,255,0.14);
    border-radius: 14px;
    padding: 10px;
    display: none;
    box-shadow: 0 15px 35px rgba(0,0,0,0.55);
    z-index: 1000;
}

.profile-menu.active {
    display: block;
}

.profile-menu-name {
    color: white;
    font-size: 0.85rem;
    font-weight: 800;
    padding: 9px 10px;
    border-bottom: 1px solid rgba(255,255,255,0.1);
    margin-bottom: 8px;
}

.profile-menu a {
    display: block;
    padding: 10px;
    border-radius: 9px;
    color: #00c2ff;
    text-decoration: none;
    font-size: 0.9rem;
    font-weight: 800;
}

.profile-menu a:hover {
    background: rgba(0,194,255,0.12);
}

/* PAGE */

.series-page,
.movie-page{
    max-width:1400px;
    margin:0 auto;
    padding:165px 35px 60px;
}

/* TABS */

.category-tabs{
    display:flex;
    gap:30px;
    overflow-x:auto;
    padding-bottom:14px;
    margin-bottom:35px;
    border-bottom:1px solid rgba(255,255,255,0.08);
}

.category-tabs::-webkit-scrollbar{
    display:none;
}

.cat-tab{
    background:none;
    border:none;
    color:#8f9aa5;
    font-weight:800;
    cursor:pointer;
    white-space:nowrap;
    padding-bottom:10px;
    position:relative;
    transition:0.25s ease;
    font-size:0.95rem;
    text-decoration:none;
}

.cat-tab:hover,
.cat-tab.active{
    color:white;
}

.cat-tab.active::after{
    content:"";
    position:absolute;
    left:0;
    bottom:-15px;
    width:100%;
    height:2px;
    background:#00c2ff;
    box-shadow:0 0 14px rgba(0,194,255,0.8);
}

/* GRID */

.series-grid{
    display:grid;
    grid-template-columns:repeat(5,1fr);
    gap:26px 20px;
}

.series-card{
    text-decoration:none;
    color:white;
}

.series-thumb{
    width:220px;
    height:330px;
    background-size:cover;
    background-position:center;
    border-radius:12px;
    background-color:#111;
    transition:0.3s ease;
    border:1px solid rgba(255,255,255,0.08);
}

.series-card:hover .series-thumb{
    transform:scale(1.03);
    border-color:rgba(0,194,255,0.35);
    box-shadow:0 0 24px rgba(0,194,255,0.18);
}

.series-title{
    margin-top:11px;
    font-size:0.95rem;
    font-weight:700;
}

.series-meta{
    margin-top:5px;
    color:#8f9aa5;
    font-size:0.78rem;
}

.series-meta span{
    color:#00c2ff;
}

.empty-message{
    color:#8f9aa5;
    font-weight:800;
}

.search-modal {
    position: fixed;
    inset: 0;
    z-index: 2000;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    pointer-events: none;
    transition: 0.35s ease;
}

.search-modal.active {
    opacity: 1;
    pointer-events: auto;
}

.search-blur {
    position: absolute;
    inset: 0;
    background: rgba(0,0,0,0.78);
    backdrop-filter: blur(0px);
    transition: 0.35s ease;
}

.search-modal.active .search-blur {
    backdrop-filter: blur(10px);
}

.search-box {
    position: relative;
    z-index: 2;
    width: 620px;
    max-width: 92%;
    transform: translateY(40px) scale(0.95);
    opacity: 0;
    transition: 0.35s cubic-bezier(.2,.8,.2,1);
}

.search-modal.active .search-box {
    transform: translateY(0) scale(1);
    opacity: 1;
}

.search-top {
    display: flex;
    align-items: center;
    margin-bottom: 12px;
}

.search-top h2 {
    margin-right: auto;
}

.close-search {
    width: 42px;
    height: 42px;
    background: #101216;
    color: white;
    border: 1px solid rgba(255,255,255,0.18);
    border-radius: 9px;
    cursor: pointer;
}

.search-input-wrap {
    height: 54px;
    background: #080a0d;
    border: 1px solid rgba(255,255,255,0.18);
    border-radius: 13px;
    display: flex;
    align-items: center;
    padding: 0 16px;
    gap: 12px;
}

.search-input-wrap input {
    flex: 1;
    background: transparent;
    border: none;
    outline: none;
    color: white;
    font-size: 1rem;
}

.clear-btn {
    background: none;
    border: none;
    color: #aaa;
    cursor: pointer;
}

.search-results {
    margin-top: 14px;
    background: rgba(5,5,5,0.94);
    border: 1px solid rgba(255,255,255,0.12);
    border-radius: 16px;
    padding: 12px;
    display: none;
}

.search-results.active {
    display: block;
}

.result-card {
    display: flex;
    gap: 14px;
    padding: 12px;
    border-radius: 12px;
    transition: 0.25s;
    text-decoration: none;
    color: inherit;
}

.result-card:hover {
    background: rgba(255,255,255,0.06);
}

.result-card img {
    width: 64px;
    height: 96px;
    object-fit: cover;
    border-radius: 6px;
}

.browse-menu {
    position: absolute;
    top: 42px;
    right: 0;
    width: 320px;
    background: rgba(8,10,13,0.96);
    border: 1px solid rgba(255,255,255,0.12);
    border-radius: 20px;
    padding: 18px 14px;
    display: none;
    box-shadow: 0 18px 45px rgba(0,0,0,0.6);
    z-index: 999;
}

.browse-menu.active {
    display: block;
}

.browse-title {
    text-align: center;
    font-weight: 900;
    margin-bottom: 20px;
}

.browse-section-label {
    color: #8f9aa5;
    font-size: 0.72rem;
    font-weight: 900;
    letter-spacing: 1px;
    margin: 16px 0 10px;
}

.browse-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 12px;
}

.browse-personal {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 12px;
}

.browse-menu a {
    width: auto;
    height: 76px;
    padding: 0;
    border-radius: 12px;
    color: white;
    text-decoration: none;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 7px;
    font-size: 0.72rem;
    font-weight: 800;
}

.browse-menu a i {
    color: #00c2ff;
    width: 24px;
    height: 24px;
}

.browse-menu a:hover {
    background: rgba(0,194,255,0.10);
}

.browse-menu a {
    width: auto;
    height: 76px;
    padding: 0;
    border-radius: 12px;
    color: #ccc;
    text-decoration: none;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 7px;
    font-size: 0.72rem;
    font-weight: 800;
    position: relative;
    transition: 0.3s;
}

.browse-menu a i {
    color: #00c2ff;
    width: 24px;
    height: 24px;
}

.browse-menu a::after {
    content: "";
    position: absolute;
    bottom: 10px;
    left: 50%;
    transform: translateX(-50%);
    width: 0%;
    height: 2px;
    background: #00c2ff;
    transition: 0.3s;
    border-radius: 999px;
}

.browse-menu a:hover {
    color: white;
    background: rgba(0,194,255,0.10);
}

.browse-menu a:hover::after {
    width: 55%;
}
</style>
</head>

<body>
<?php include "../Preloader.php"; ?>
<header class="header">

    <div class="left">
        <img src="../Assets/Logo.png" class="logo-img">
    </div>

    <div class="right">

        <div class="nav">

            <a href="../Index.php" class="home-btn">
                <i data-lucide="house"></i>
            </a>

            <div class="browse-dropdown">

                <button type="button" class="browse-btn" onclick="toggleBrowseMenu(event)">
                    <i data-lucide="grid-2x2"></i>
                    <i data-lucide="chevron-down" class="browse-arrow"></i>
                </button>

                <div class="browse-menu" id="browseMenu">
                    <div class="browse-title">Browse</div>
                
                    <div class="browse-section-label">CONTENT</div>
                    <div class="browse-grid">
                        <a href="../Movies/Movies.php">
                            <i data-lucide="clapperboard"></i>
                            Movies
                        </a>
                
                        <a href="../Series/Series.php">
                            <i data-lucide="tv"></i>
                            TV Shows
                        </a>
                
                        <a href="../PPV/PPV.php">
                            <i data-lucide="ticket"></i>
                            PPV
                        </a>
                    </div>
                
                    <div class="browse-section-label">PERSONAL</div>
                    <div class="browse-personal">
                        <a href="../History.php">
                            <i data-lucide="history"></i>
                            History
                        </a>
                
                        <a href="../Watchlist.php">
                            <i data-lucide="heart"></i>
                            Watchlist
                        </a>
                    </div>
                </div>
            </div>
        </div>

        <button type="button" class="search-icon-btn" onclick="openSearch()">
            <i data-lucide="search"></i>
        </button>

        <div class="profile-dropdown" onclick="toggleProfileMenu()">
        
            <img src="<?php echo htmlspecialchars($avatar); ?>" class="profile-avatar">
        
            <div class="profile-menu" id="profileMenu">
        
                <div class="profile-menu-name">
                    <?php echo htmlspecialchars($username); ?>
                </div>
        
                <a href="../Index.php">Back To Home</a>
                <a href="../Auth/logout.php">Logout</a>
        
            </div>
        
        </div>

    </div>

</header>

<div class="series-page">

    <div class="category-tabs">

        <?php
        $categories = [
            "Most Popular",
            "Most Rating",
            "Most Recent",
            "Action",
            "Adventure",
            "Comedy",
            "Crime",
            "Drama",
            "Fantasy",
            "Horror"
        ];
        ?>

        <?php foreach ($categories as $cat): ?>

            <a
                href="Series.php?category=<?php echo urlencode($cat); ?>"
                class="cat-tab <?php echo $selectedCategory === $cat ? 'active' : ''; ?>"
            >
                <?php echo htmlspecialchars($cat); ?>
            </a>

        <?php endforeach; ?>

    </div>

    <div class="series-grid">

        <?php foreach ($series as $show): ?>

            <?php
            $thumb = !empty($show["banner"])
                ? "../" . $show["banner"]
                : (!empty($show["logo"])
                    ? "../" . $show["logo"]
                    : "../Assets/Logo.png");
            ?>

            <a href="Preset.php?id=<?php echo $show["id"]; ?>" class="series-card">

                <div
                    class="series-thumb"
                    style="background-image:url('<?php echo htmlspecialchars($thumb); ?>')"
                ></div>

                <div class="series-title">
                    <?php echo htmlspecialchars($show["title"]); ?>
                </div>

                <div class="series-meta">
                    <span>★</span>
                    <?php echo htmlspecialchars($show["rating"] ?? "N/A"); ?>
                    ·
                    <?php echo htmlspecialchars($show["release_year"] ?? "Unknown"); ?>
                    · TV Show
                </div>

            </a>

        <?php endforeach; ?>

        <?php if (empty($series)): ?>

            <div class="empty-message">
                No TV Shows Found.
            </div>

        <?php endif; ?>
    </div>
</div>

<div class="search-modal" id="searchModal">
    <div class="search-blur" onclick="closeSearch()"></div>

    <div class="search-box">
        <div class="search-top">
            <h2>Search</h2>
            <button class="close-search" onclick="closeSearch()">✕</button>
        </div>

        <div class="search-input-wrap">
            <i data-lucide="search"></i>
            <input type="text" id="searchInput" placeholder="Type here to search..." oninput="showResults()">
            <button onclick="clearSearch()" class="clear-btn">✕</button>
        </div>

        <div class="search-results" id="searchResults">
        
            <?php foreach ($series as $show): ?>
                <?php
                $searchThumb = !empty($show["banner"])
                    ? "../" . $show["banner"]
                    : (!empty($show["logo"])
                        ? "../" . $show["logo"]
                        : "https://picsum.photos/220/330?random=" . $show["id"]);
                ?>
        
                <a href="Preset.php?id=<?php echo $show["id"]; ?>" class="result-card">
                    <img src="<?php echo htmlspecialchars($searchThumb); ?>">
        
                    <div class="result-info">
                        <h3><?php echo htmlspecialchars($show["title"]); ?></h3>
        
                        <p>
                            Movie |
                            <?php echo htmlspecialchars($show["release_year"] ?? "Unknown"); ?>
                            |
                            ⭐ <?php echo htmlspecialchars($show["rating"] ?? "N/A"); ?>
                            |
                            <?php echo htmlspecialchars($show["category"] ?? "Uncategorized"); ?>
                        </p>
                    </div>
                </a>
        
            <?php endforeach; ?>
        
        </div>
    </div>
</div>

<script>
lucide.createIcons();

function toggleBrowseMenu(event){

    event.stopPropagation();

    document
        .getElementById("browseMenu")
        .classList
        .toggle("active");
}

document.addEventListener("click", function(e){

    const browseMenu = document.getElementById("browseMenu");

    const browseDropdown =
        document.querySelector(".browse-dropdown");

    if (
        browseMenu &&
        browseDropdown &&
        !browseDropdown.contains(e.target)
    ) {
        browseMenu.classList.remove("active");
    }
});

const searchModal = document.getElementById("searchModal");
const searchInput = document.getElementById("searchInput");
const searchResults = document.getElementById("searchResults");

function openSearch() {
    searchModal.classList.add("active");
    setTimeout(() => searchInput.focus(), 100);
}

function closeSearch() {
    searchModal.classList.remove("active");

    setTimeout(() => {
        searchResults.classList.remove("active");
        searchInput.value = "";
    }, 300);
}

function showResults() {

    const term = searchInput.value.trim().toLowerCase();

    const cards =
        document.querySelectorAll("#searchResults .result-card");

    searchResults.classList.toggle("active", term.length > 0);

    cards.forEach(card => {

        const title =
            card.querySelector("h3").textContent.toLowerCase();

        const meta =
            card.querySelector("p").textContent.toLowerCase();

        if (title.includes(term) || meta.includes(term)) {
            card.style.display = "flex";
        } else {
            card.style.display = "none";
        }
    });
}

function clearSearch() {

    searchInput.value = "";

    searchResults.classList.remove("active");

    document
        .querySelectorAll("#searchResults .result-card")
        .forEach(card => {
            card.style.display = "flex";
        });

    searchInput.focus();
}

function toggleProfileMenu() {
    document
        .getElementById("profileMenu")
        .classList
        .toggle("active");
}

document.addEventListener("click", function(e) {

    const dropdown =
        document.querySelector(".profile-dropdown");

    const menu =
        document.getElementById("profileMenu");

    if (dropdown && menu && !dropdown.contains(e.target)) {
        menu.classList.remove("active");
    }
});

document.addEventListener("keydown", function(e) {
    if (e.key === "Escape") closeSearch();
});
</script>
<script src="../Security.js"></script>
</body>
</html>