@extends('front.layout')
@section('title', $show->seo_title ?: $show->title)
@section('description', $show->meta_description ?: \Illuminate\Support\Str::limit(strip_tags($show->overview), 155))

@section('content')
@php
    $media = app(\App\Services\MediaStorageService::class);
    $backdropUrl = $media->url($show->backdrop_path);
    $posterUrl = $media->url($show->poster_path) ?: 'https://placehold.co/600x900?text=No+Poster';
    $playerAd = ($ads['player_sponsor'] ?? collect())->first();
@endphp

<div class="movie-detail-page">
    <section class="movie-detail-hero" style="--movie-backdrop: url('{{ $backdropUrl }}')">
        <div class="movie-detail-backdrop"></div>
        <div class="movie-detail-overlay"></div>
        <div class="container movie-detail-content">
            <div class="movie-poster-wrap">
                <img class="movie-detail-poster" src="{{ $posterUrl }}" alt="{{ $show->title }} poster">
                @if ($show->vote_average)
                    <div class="poster-rating"><span>★</span> {{ number_format((float) $show->vote_average, 1) }}</div>
                @endif
            </div>
            <div class="movie-detail-copy">
                <div class="detail-eyebrow"><span>Web series</span></div>
                <h1>{{ $show->title }}</h1>
                <div class="movie-meta">
                    @if ($show->first_air_date)<span>{{ $show->first_air_date->format('Y') }}</span>@endif
                    <span>{{ $show->seasons->where('is_active', 1)->count() }} seasons</span>
                </div>
                @if ($show->genres->isNotEmpty())
                    <div class="chips movie-chips">
                        @foreach ($show->genres as $genre)<a href="{{ route('genre', $genre) }}">{{ $genre->name }}</a>@endforeach
                    </div>
                @endif
                <p class="movie-overview">{{ $show->overview ?: 'No overview is available for this series yet.' }}</p>
                <div class="movie-actions"><a class="browse-btn" href="{{ route('shows') }}">Browse web series</a></div>
            </div>
        </div>
    </section>

    <div class="container detail-body-grid player-layout">
        <div class="detail-main-column">
            @include('front.partials.ad', ['slot' => 'details_top'])
            <div id="video-player"></div>
            @include('front.partials.embed-player', [
                'title' => $selectedEpisode
                    ? $show->title.' — S'.$selectedEpisode->season->season_number.' E'.$selectedEpisode->episode_number
                    : $show->title,
                'embedUrl' => $embedUrl ?? null,
            ])

            @php $showRuntime = optional($selectedEpisode)->runtime; @endphp
            @include('front.partials.server-switcher', [
                'embedProviders' => $embedProviders ?? [],
                'selectedServer' => $selectedServer ?? null,
            ])

            @include('front.partials.player-stats', [
                'statsType' => 'show',
                'statsId' => $show->id,
                'statsDate' => optional($show->first_air_date)->format('M d Y'),
                'statsRuntime' => $showRuntime ? intdiv((int) $showRuntime, 60).'h '.((int) $showRuntime % 60).'m' : null,
                'statsRating' => $show->vote_average,
            ])

            @include('front.partials.reactions', [
                'reactionType' => 'show',
                'reactionId' => $show->id,
            ])

            @include('front.partials.top-cast', ['castItems' => $show->top_cast ?? []])

            <section class="seasons-section">
                <div class="section-head"><h2>Seasons & Episodes</h2></div>
                @forelse ($show->seasons->where('is_active', 1) as $season)
                    <details class="season">
                        <summary>{{ $season->title }} <small>{{ $season->episodes->where('is_active', 1)->count() }} episodes</small></summary>
                        <div class="episodes">
                            @foreach ($season->episodes->where('is_active', 1) as $episode)
                                @php
                                    $episodeUrl = route('show', $show) . '?' . http_build_query([
                                        'season' => $season->season_number,
                                        'episode' => $episode->episode_number,
                                        'server' => $selectedServer,
                                    ]);
                                    $isSelected = optional($selectedEpisode)->id === $episode->id;
                                @endphp
                                <a class="episode-play-card {{ $isSelected ? 'is-active' : '' }}" href="{{ $episodeUrl }}#video-player">
                                    <div>
                                        <h3>E{{ $episode->episode_number }} · {{ $episode->title }}</h3>
                                        <p>{{ $episode->overview ?: 'Watch this episode.' }}</p>
                                    </div>
                                    <span>{{ $isSelected ? 'Playing' : 'Play' }} ▶</span>
                                </a>
                            @endforeach
                        </div>
                    </details>
                @empty
                    <div class="premium-panel"><p>Season information will be added soon.</p></div>
                @endforelse
            </section>
            @include('front.partials.ad', ['slot' => 'details_bottom'])
        </div>

        <aside class="detail-sidebar related-sidebar">
            <section class="related-panel premium-panel">
                <div class="sidebar-title"><span></span><h2>More web series</h2></div>
                @if (($relatedShows ?? collect())->isNotEmpty())
                    <div class="related-list related-list-large">
                        @foreach ($relatedShows as $related)
                            <a href="{{ route('show', $related) }}">
                                <img src="{{ $media->url($related->poster_path) ?: 'https://placehold.co/160x240?text=No+Poster' }}" alt="{{ $related->title }}">
                                <div>
                                    <strong>{{ $related->title }}</strong>
                                    <small>{{ optional($related->first_air_date)->format('Y') ?: 'Coming soon' }} <b>★ {{ $related->vote_average ?: 'N/A' }}</b></small>
                                    @if ($related->genres->isNotEmpty())<span>{{ $related->genres->take(2)->pluck('name')->join(' · ') }}</span>@endif
                                </div>
                            </a>
                        @endforeach
                    </div>
                @else
                    <p class="sidebar-empty">More web series will appear here.</p>
                @endif
                <a class="sidebar-browse-all" href="{{ route('shows') }}">Browse all web series <span>→</span></a>
            </section>
        </aside>
    </div>
</div>
@endsection
