<?php
session_start();
require "../Auth/db.php";

if (empty($_SESSION["user"]["is_admin"])) {
    header("Location: ../Index.php");
    exit;
}

$stmt = $pdo->query("SELECT * FROM media_series ORDER BY id DESC");
$series = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>

<h1>DB TV Shows <span>› Admin Interface</span></h1>

<div class="panel">
    <div class="panel-head">
        <h2>TV Shows Database</h2>
        <a href="CreateSeries.php" class="btn-add">Add TV Show</a>
    </div>

    <table class="db-table">
        <thead>
            <tr>
                <th>TV Show</th>
                <th>TMDB ID</th>
                <th>Category</th>
                <th>Rating</th>
                <th>Actions</th>
            </tr>
        </thead>

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

                <tr>
                    <td class="movie-cell">
                        <img src="<?php echo htmlspecialchars($thumb); ?>">
                        <div>
                            <strong><?php echo htmlspecialchars($show["title"]); ?></strong>
                            <small>
                                <?php echo htmlspecialchars($show["release_year"] ?? "Unknown"); ?>
                                · ⭐ <?php echo htmlspecialchars($show["rating"] ?? "N/A"); ?>
                            </small>
                        </div>
                    </td>

                    <td><?php echo htmlspecialchars($show["tmdb_id"] ?? "N/A"); ?></td>

                    <td>
                        <span class="pill">
                            <?php echo htmlspecialchars($show["category"] ?? "Uncategorized"); ?>
                        </span>
                    </td>

                    <td><?php echo htmlspecialchars($show["rating"] ?? "N/A"); ?></td>

                    <td class="actions">
                        <a href="EditSeries.php?id=<?php echo $show["id"]; ?>" class="edit-btn">Edit</a>

                        <a
                            href="DeleteSeries.php?id=<?php echo $show["id"]; ?>"
                            class="delete-btn"
                            onclick="return confirm('Delete this TV show?');"
                        >
                            Delete
                        </a>
                    </td>
                </tr>
            <?php endforeach; ?>

            <?php if (empty($series)): ?>
                <tr>
                    <td colspan="5">No TV shows found.</td>
                </tr>
            <?php endif; ?>
        </tbody>
    </table>
</div>

<style>
.panel {
    background:#080d10;
    border:1px solid rgba(0,194,255,0.18);
    border-radius:16px;
    padding:25px;
}

.panel-head {
    display:flex;
    justify-content:space-between;
    align-items:center;
    margin-bottom:20px;
}

.btn-add {
    background:#00c2ff;
    color:#001014;
    padding:12px 18px;
    border-radius:10px;
    text-decoration:none;
    font-weight:900;
}

.db-table {
    width:100%;
    border-collapse:collapse;
}

.db-table th,
.db-table td {
    padding:16px;
    border-bottom:1px solid rgba(255,255,255,0.08);
    text-align:left;
}

.db-table th {
    color:#8f9aa5;
    font-size:0.75rem;
    letter-spacing:1px;
    text-transform:uppercase;
}

.movie-cell {
    display:flex;
    align-items:center;
    gap:12px;
}

.movie-cell img {
    width:45px;
    height:45px;
    border-radius:8px;
    object-fit:cover;
}

.movie-cell small {
    display:block;
    color:#8f9aa5;
    margin-top:4px;
}

.pill {
    background:rgba(0,194,255,0.13);
    color:#00c2ff;
    padding:7px 12px;
    border-radius:999px;
    font-weight:800;
    font-size:0.78rem;
}

.actions {
    display:flex;
    gap:10px;
}

.edit-btn,
.delete-btn {
    padding:9px 13px;
    border-radius:9px;
    text-decoration:none;
    font-weight:900;
}

.edit-btn {
    background:#00c2ff;
    color:#001014;
}

.delete-btn {
    background:rgba(255,60,60,0.15);
    color:#ff6666;
    border:1px solid rgba(255,60,60,0.35);
}
</style>