<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{{ show['SHOW'] }}</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #141414;
            color: #fff;
            margin: 0;
            padding: 0;
        }

        .container {
            max-width: 800px;
            margin: auto;
            padding: 20px;
            text-align: center;
        }

        img {
            max-width: 100%;
            height: auto;
            border-radius: 6px;
        }

        .title {
            font-size: 2em;
            margin-top: 20px;
        }

        .description {
            font-size: 1em;
            color: #ccc;
            margin: 15px 0;
        }

        .season-grid {
            display: flex;
            flex-wrap: wrap;
            justify-content: center;
            margin-top: 20px;
            gap: 10px;
        }

        .season-tile {
            background-color: #222;
            border: 1px solid #333;
            padding: 10px 15px;
            cursor: pointer;
            border-radius: 5px;
            transition: background-color 0.3s;
        }

        .season-tile:hover {
            background-color: #333;
        }

        .watch-btn {
            margin-top: 30px;
            padding: 12px 25px;
            font-size: 1em;
            background-color: #e50914;
            color: #fff;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }

        .watch-btn:hover {
            background-color: #b20710;
        }

        a {
            color: #ccc;
            display: inline-block;
            margin-top: 20px;
        }

        @media (max-width: 600px) {
            .season-tile {
                flex: 1 1 100%;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <img src="{{ show['POSTER'] }}" alt="{{ show['SHOW'] }}">
        <div class="title">{{ show['SHOW'] }}</div>
        <div class="description">{{ show['DESCRIPTION'] }}</div>

        <div class="season-grid">
            {% for season in show['SEASONS'] %}
                <div class="season-tile" onclick="downloadSeason('{{ season }}')">
                    Season {{ season }}
                </div>
            {% endfor %}
        </div>

        <button class="watch-btn" onclick="watchShow()">▶ Watch Now</button>

        <p><a href="/">← Back to all shows</a></p>
    </div>

    <script>
        function downloadSeason(season) {
            const formData = new FormData();
            formData.append("show", "{{ show['SHOW'] }}");
            formData.append("season", parseInt(season, 10));

            fetch('/dropout/download', {
                method: 'POST',
                body: formData
            })
            .then(response => {
                if (!response.ok) throw new Error('Network error');
                return response.json();
            })
            .then(data => {
                alert(`Season ${season} downloading.`);
            })
            .catch(err => {
                console.error(err);
                alert("Error downloading season.");
            });
        }


        function watchShow() {
            fetch('/watch', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({ show: "{{ show['SHOW'] }}" })
            })
            .then(response => {
                if (!response.ok) throw new Error('Network error');
                return response.json();
            })
            .then(data => {
                alert('Playback started!');
                // Optionally redirect to video player
            })
            .catch(err => {
                console.error(err);
                alert("Error starting playback.");
            });
        }
    </script>
</body>
</html>
