<?= $this->extend('layouts/main') ?>

<?= $this->section('title') ?><?= esc($pelicula->titulo) ?><?= $this->endSection() ?>

<?= $this->section('content') ?>
<style>
    .movie-detail-grid {
        display: grid;
        grid-template-columns: 300px 1fr;
        gap: 40px;
        align-items: start;
        margin-top: 10px;
    }

    @media (max-width: 900px) {
        .movie-detail-grid {
            grid-template-columns: 1fr;
        }
    }

    /* ---- Columna izquierda ---- */
    .movie-poster-wrap {
        position: relative;
        border-radius: 10px;
        overflow: hidden;
        box-shadow: 0 10px 30px rgba(0,0,0,0.5);
    }

    .movie-poster-wrap img {
        width: 100%;
        display: block;
    }

    .poster-badge {
        position: absolute;
        top: 10px;
        background: rgba(0,0,0,0.75);
        color: #fff;
        font-size: 0.75rem;
        font-weight: 700;
        padding: 4px 8px;
        border-radius: 4px;
        letter-spacing: 0.03em;
    }

    .poster-badge.rating { left: 10px; }
    .poster-badge.duration { right: 10px; }

    .poster-play-btn {
        position: absolute;
        bottom: 12px;
        right: 12px;
        width: 42px;
        height: 42px;
        border-radius: 50%;
        background: #e50914;
        display: flex;
        align-items: center;
        justify-content: center;
        color: #fff;
        text-decoration: none;
        font-size: 1.1rem;
        box-shadow: 0 4px 12px rgba(0,0,0,0.5);
        transition: transform 0.2s ease;
    }

    .poster-play-btn:hover {
        transform: scale(1.08);
    }

    .movie-action-btn {
        display: block;
        width: 100%;
        margin-top: 14px;
        padding: 10px;
        text-align: center;
        border-radius: 999px;
        border: 1px solid rgba(255,255,255,0.25);
        background: transparent;
        color: #fff;
        font-weight: 700;
        letter-spacing: 0.03em;
        text-decoration: none;
        font-size: 0.85rem;
    }

    .movie-action-btn:hover {
        border-color: var(--primary);
        color: var(--primary);
    }

    .movie-synopsis {
        margin-top: 16px;
        color: #ccc;
        font-size: 0.9rem;
        line-height: 1.6;
    }

    .movie-facts {
        margin-top: 20px;
        border-top: 1px solid rgba(255,255,255,0.1);
    }

    .movie-fact-row {
        display: flex;
        justify-content: space-between;
        gap: 10px;
        padding: 12px 0;
        border-bottom: 1px solid rgba(255,255,255,0.08);
        font-size: 0.82rem;
    }

    .movie-fact-row .label {
        color: #888;
        text-transform: uppercase;
        font-weight: 700;
        letter-spacing: 0.04em;
        white-space: nowrap;
    }

    .movie-fact-row .value {
        color: #ddd;
        text-align: right;
    }

    .back-cartelera-link {
        display: inline-block;
        margin-top: 20px;
        color: var(--primary);
        font-size: 0.85rem;
        text-decoration: none;
    }

    /* ---- Columna derecha: horarios ---- */
    .horarios-column {
        background: rgba(255,255,255,0.02);
        border: 1px solid rgba(255,255,255,0.08);
        border-radius: 24px;
        padding: 26px;
    }

    .horarios-title {
        font-size: 1.6rem;
        font-weight: 800;
        letter-spacing: 0.02em;
        margin: 0 0 20px 0;
        text-transform: uppercase;
        color: #fff;
    }

    .date-tabs {
        display: flex;
        gap: 10px;
        flex-wrap: wrap;
        margin-bottom: 20px;
    }

    .date-tab {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        min-width: 82px;
        padding: 10px 14px;
        border-radius: 14px;
        border: 1px solid rgba(255,255,255,0.12);
        background: rgba(255,255,255,0.04);
        color: #cfcfcf;
        text-decoration: none;
        font-size: 0.8rem;
        line-height: 1.3;
    }

    .date-tab strong {
        font-size: 0.95rem;
        color: #fff;
        text-transform: uppercase;
    }

    .date-tab.active {
        background: rgba(255,255,255,0.95);
        border-color: rgba(255,255,255,0.65);
        box-shadow: 0 18px 45px rgba(0,0,0,0.18);
    }

    .date-tab.active,
    .date-tab.active strong {
        color: #111;
    }

    .availability-legend {
        display: flex;
        flex-wrap: wrap;
        gap: 16px;
        margin: 18px 0 24px 0;
        align-items: center;
    }

    .availability-legend .legend-title {
        width: 100%;
        text-transform: uppercase;
        font-size: 0.72rem;
        letter-spacing: 0.05em;
        color: #888;
        font-weight: 700;
        margin-bottom: 4px;
    }

    .availability-legend .legend-entry {
        display: flex;
        align-items: center;
        gap: 8px;
        font-size: 0.82rem;
        color: #ccc;
    }

    .dot {
        width: 10px;
        height: 10px;
        border-radius: 50%;
        display: inline-block;
    }

    .dot.alta { background: #4caf50; }
    .dot.media { background: #f4b400; }
    .dot.baja { background: #ff5722; }
    .dot.lleno { background: #666; }

    .cine-block {
        background: rgba(255,255,255,0.03);
        border: 1px solid rgba(255,255,255,0.08);
        border-radius: 20px;
        padding: 20px;
        margin-bottom: 18px;
    }

    .cine-block .cine-eyebrow {
        text-transform: uppercase;
        font-size: 0.72rem;
        letter-spacing: 0.05em;
        color: #aaa;
        font-weight: 700;
    }

    .cine-block .cine-name {
        margin: 4px 0 6px 0;
        font-size: 1.2rem;
        font-weight: 800;
        text-transform: uppercase;
        color: var(--primary);
    }

    .cine-block .cine-address {
        font-size: 0.85rem;
        color: #b5b5be;
        margin-bottom: 18px;
    }

    .formato-block {
        background: rgba(255,255,255,0.04);
        border: 1px solid rgba(255,255,255,0.1);
        border-radius: 24px;
        padding: 22px;
        margin-bottom: 22px;
    }

    .formato-header {
        display: flex;
        flex-wrap: wrap;
        justify-content: space-between;
        gap: 16px;
        align-items: center;
        margin-bottom: 18px;
    }

    .formato-title {
        font-size: 1rem;
        color: #fff;
        font-weight: 900;
        text-transform: uppercase;
        letter-spacing: 0.06em;
    }

    .formato-subtitle {
        color: #c0c0cc;
        font-size: 0.88rem;
        margin-top: 4px;
    }

    .horarios-grid {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
        gap: 14px;
    }

    .horario-card {
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        padding: 18px;
        border-radius: 20px;
        border: 1px solid rgba(255,255,255,0.08);
        background: rgba(255,255,255,0.06);
        color: #fff;
        text-decoration: none;
        min-height: 140px;
        transition: transform 0.2s ease, border-color 0.2s ease, background 0.2s ease, box-shadow 0.2s ease;
    }

    .horario-card:hover {
        transform: translateY(-2px);
        background: rgba(255,255,255,0.14);
        border-color: rgba(229,18,31,0.4);
        box-shadow: 0 18px 30px rgba(0,0,0,0.12);
    }

    .horario-card.lleno {
        opacity: 0.45;
        pointer-events: none;
        background: rgba(255,255,255,0.02);
        border-color: rgba(255,255,255,0.06);
    }

    .horario-time {
        display: block;
        font-size: 1.4rem;
        font-weight: 900;
        margin-bottom: 10px;
        color: #fff;
    }

    .horario-slot {
        color: #c2c2d0;
        font-size: 0.92rem;
        margin-bottom: 16px;
        min-height: 32px;
    }

    .horario-meta {
        display: flex;
        justify-content: space-between;
        align-items: center;
        gap: 10px;
        font-size: 0.85rem;
        color: #c5c5d0;
    }

    .horario-status {
        display: inline-flex;
        align-items: center;
        gap: 8px;
        padding: 6px 10px;
        border-radius: 999px;
        background: rgba(255,255,255,0.1);
        color: #f2f2f8;
    }

    .horario-price {
        color: #fff;
        font-weight: 700;
    }

    .cines-cercanos-box {
        background: rgba(255,255,255,0.05);
        border: 1px solid rgba(255,255,255,0.1);
        border-radius: 14px;
        padding: 18px 20px;
        margin-top: 12px;
    }

    .cines-cercanos-box h4 {
        margin: 0 0 8px 0;
        font-size: 0.9rem;
        text-transform: uppercase;
    }

    .cines-cercanos-box p {
        margin: 0;
        font-size: 0.85rem;
        color: #aaa;
    }

    .cines-cercanos-box a {
        color: var(--primary);
    }
</style>

<div class="movie-detail-grid">

    <!-- ============ COLUMNA IZQUIERDA ============ -->
    <div class="movie-info-column">
        <div class="movie-poster-wrap">
            <img src="<?= $pelicula->getPosterUrl() ?>" alt="<?= esc($pelicula->titulo) ?>">
            <span class="poster-badge rating"><?= esc($pelicula->clasificacion) ?></span>
            <?php
                $horas = intdiv($pelicula->duracion_min, 60);
                $minutos = $pelicula->duracion_min % 60;
            ?>
            <span class="poster-badge duration"><?= $horas ?>h <?= $minutos ?>m</span>

            <?php if (!empty($pelicula->getTrailerEmbedUrl())): ?>
                <a href="#trailer" class="poster-play-btn" title="Ver tráiler">▶</a>
            <?php endif; ?>
        </div>

        <?php if (!empty($pelicula->getTrailerEmbedUrl())): ?>
            <a href="#trailer" class="movie-action-btn">▶ Ver Tráiler</a>
        <?php endif; ?>

        <h1 style="font-size:1.5rem; margin: 18px 0 4px 0;"><?= esc($pelicula->titulo) ?></h1>

        <p class="movie-synopsis">
            <?= !empty($pelicula->sinopsis) ? nl2br(esc($pelicula->sinopsis)) : 'Sin sinopsis disponible.' ?>
        </p>

        <div class="movie-facts">
            <?php if (!empty($formatosDisponibles)): ?>
                <div class="movie-fact-row">
                    <span class="label">Formatos disponibles</span>
                    <span class="value"><?= esc(implode(' · ', $formatosDisponibles)) ?></span>
                </div>
            <?php endif; ?>
            <div class="movie-fact-row">
                <span class="label">Duración</span>
                <span class="value"><?= $horas ?>h <?= $minutos ?>m</span>
            </div>
            <div class="movie-fact-row">
                <span class="label">Género</span>
                <span class="value"><?= esc($pelicula->genero_nombre) ?></span>
            </div>
            <div class="movie-fact-row">
                <span class="label">Clasificación</span>
                <span class="value"><?= esc($pelicula->clasificacion) ?></span>
            </div>
            <div class="movie-fact-row">
                <span class="label">Idioma</span>
                <span class="value"><?= esc($pelicula->idioma) ?></span>
            </div>
        </div>

        <a href="<?= base_url('/') ?>" class="back-cartelera-link">← Volver a la Cartelera</a>
    </div>

    <!-- ============ COLUMNA DERECHA: HORARIOS ============ -->
    <div class="horarios-column">
        <h2 class="horarios-title">Horarios</h2>

        <?php if (empty($fechasDisponibles)): ?>
            <div class="empty-state">No hay funciones programadas para esta película en este momento.</div>
        <?php else: ?>

            <!-- Pestañas de fecha -->
            <div class="date-tabs">
                <?php
                    $hoy = date('Y-m-d');
                    $diasSemana = ['Dom', 'Lun', 'Mar', 'Mié', 'Jue', 'Vie', 'Sáb'];
                    $meses = ['ene','feb','mar','abr','may','jun','jul','ago','sep','oct','nov','dic'];
                ?>
                <?php foreach ($fechasDisponibles as $fecha): ?>
                    <?php
                        $ts = strtotime($fecha);
                        $esHoy = $fecha === $hoy;
                        $activo = $fecha === $fechaSeleccionada;
                        $diaLabel = $esHoy ? 'Hoy' : $diasSemana[date('w', $ts)];
                        $fechaLabel = date('j', $ts) . '/' . $meses[date('n', $ts) - 1];
                    ?>
                    <a href="?fecha=<?= $fecha ?>" class="date-tab <?= $activo ? 'active' : '' ?>">
                        <strong><?= esc($diaLabel) ?></strong>
                        <?= esc($fechaLabel) ?>
                    </a>
                <?php endforeach; ?>
            </div>

            <!-- Leyenda de disponibilidad de asientos -->
            <div class="availability-legend">
                <span class="legend-title">Disponibilidad de asientos</span>
                <span class="legend-entry"><span class="dot alta"></span> Alta</span>
                <span class="legend-entry"><span class="dot media"></span> Media</span>
                <span class="legend-entry"><span class="dot baja"></span> Baja</span>
                <span class="legend-entry"><span class="dot lleno"></span> Lleno</span>
            </div>

            <?php if (empty($porFormato)): ?>
                <div class="empty-state">No hay horarios para la fecha seleccionada.</div>
            <?php else: ?>
                <?php foreach ($porFormato as $formato): ?>
                    <?php
                        $precios = array_map(fn($fn) => (float) $fn->precio_base, $formato['funciones']);
                        $precioMin = number_format(min($precios), 2);
                    ?>
                    <div class="formato-block">
                        <div class="formato-header">
                            <div>
                                <div class="formato-title"><?= esc($formato['tipo']) ?></div>
                                <div class="formato-subtitle">Desde <?= count($formato['funciones']) ?> horarios disponibles</div>
                            </div>
                            <span class="price-pill">Desde S/ <?= $precioMin ?></span>
                        </div>
                        <div class="horarios-grid">
                            <?php foreach ($formato['funciones'] as $fn): ?>
                                <?php
                                    $pct = $fn->capacidad > 0 ? ($fn->vendidos / $fn->capacidad) : 0;
                                    if ($pct >= 1) {
                                        $disp = 'lleno';
                                    } elseif ($pct >= 0.7) {
                                        $disp = 'baja';
                                    } elseif ($pct >= 0.3) {
                                        $disp = 'media';
                                    } else {
                                        $disp = 'alta';
                                    }
                                ?>
                                <a href="<?= base_url('reserva/asientos/' . $fn->id_funcion) ?>"
                                   class="horario-card <?= $disp === 'lleno' ? 'lleno' : '' ?>"
                                   title="Disponibilidad: <?= ucfirst($disp) ?> · S/ <?= number_format($fn->precio_base, 2) ?>">
                                    <span class="horario-time"><?= date('H:i', strtotime($fn->hora_inicio)) ?></span>
                                    <div class="horario-slot"><?= esc($fn->local_nombre) ?> · Sala <?= esc($fn->sala_nombre) ?></div>
                                    <div class="horario-meta">
                                        <span class="horario-status"><span class="dot <?= $disp ?>"></span><?= ucfirst($disp) ?></span>
                                        <span class="horario-price">S/ <?= number_format($fn->precio_base, 2) ?></span>
                                    </div>
                                </a>
                            <?php endforeach; ?>
                        </div>
                    </div>
                <?php endforeach; ?>
            <?php endif; ?>

            <div class="cines-cercanos-box">
                <h4>Cines más cercanos</h4>
                <p>¿Buscas otro local? <a href="<?= base_url('cines') ?>">Ver todos los cines</a> con horarios disponibles para esta película.</p>
            </div>

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

<?php if (!empty($pelicula->getTrailerEmbedUrl())): ?>
    <section id="trailer" style="margin-top: 40px;">
        <h3>Ver Tráiler</h3>
        <iframe
            width="560"
            height="315"
            src="<?= $pelicula->getTrailerEmbedUrl() ?>"
            title="Trailer de <?= esc($pelicula->titulo) ?>"
            allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
            allowfullscreen
            style="border: 1px solid #333; margin-top: 10px; max-width: 100%;">
        </iframe>
    </section>
<?php endif; ?>
<?= $this->endSection() ?>
