@extends('admin.layouts.app')
@section('title', $anime->title)
@section('page-title', $anime->title)

@section('content')

<div class="d-flex align-items-center gap-3 mb-4 flex-wrap">
    <a href="{{ route('admin.animes.index') }}" class="btn btn-secondary btn-sm">
        <i class="bi bi-arrow-left"></i> Animelere Dön
    </a>
    <div style="margin-left:auto;display:flex;gap:8px;flex-wrap:wrap">
        <button class="btn btn-outline-primary btn-sm" data-bs-toggle="modal" data-bs-target="#addSeasonModal">
            <i class="bi bi-plus-circle"></i> Sezon Ekle
        </button>
        <a href="{{ route('admin.animes.edit', $anime) }}" class="btn btn-primary btn-sm">
            <i class="bi bi-pencil"></i> Düzenle
        </a>
    </div>
</div>

<div class="row g-4">

    {{-- ── Sol: Sezonlar & Bölümler ── --}}
    <div class="col-lg-8">
        @forelse($anime->seasons as $season)
        <div class="card mb-3">
            <div class="card-header">
                <div>
                    <span style="font-size:14px;font-weight:700;color:var(--text)">{{ $season->season_number }}. Sezon</span>
                    @if($season->title)
                        <span style="font-size:13px;color:var(--text2);margin-left:6px">— {{ $season->title }}</span>
                    @endif
                    @if($season->mal_id)
                        <span class="badge bg-success ms-2" style="font-size:10px">MAL: {{ $season->mal_id }}</span>
                    @endif
                </div>
                <div style="margin-left:auto;display:flex;gap:7px">
                    <a href="{{ route('admin.episodes.create', ['anime_id'=>$anime->id,'season_id'=>$season->id]) }}"
                       class="btn btn-sm btn-success">
                        <i class="bi bi-plus-lg"></i> Bölüm Ekle
                    </a>
                    <form method="POST" action="{{ route('admin.seasons.destroy', $season) }}"
                          onsubmit="return confirm('{{ $season->season_number }}. sezon ve tüm bölümleri silinecek. Devam edilsin mi?')">
                        @csrf @method('DELETE')
                        <button type="submit" class="btn btn-sm btn-outline-danger">
                            <i class="bi bi-trash3"></i>
                        </button>
                    </form>
                </div>
            </div>
            @if($season->episodes->count() > 0)
            <div class="table-responsive">
                <table class="table">
                    <thead>
                        <tr>
                            <th style="padding-left:20px">Bölüm</th>
                            <th>Başlık</th>
                            <th style="text-align:center">Durum</th>
                            <th style="text-align:center">İzlenme</th>
                            <th style="text-align:right;padding-right:20px"></th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($season->episodes as $ep)
                        <tr>
                            <td style="padding-left:20px">
                                <span style="font-family:monospace;font-size:12.5px;background:var(--accent-dim);color:var(--accent-lt);padding:2px 8px;border-radius:5px">
                                    {{ str_pad($ep->episode_number,2,'0',STR_PAD_LEFT) }}
                                </span>
                            </td>
                            <td style="font-size:13px;color:var(--text2)">{{ $ep->title ?: '—' }}</td>
                            <td style="text-align:center">
                                @if($ep->is_published)
                                    <span class="badge bg-success">Yayında</span>
                                @else
                                    <span class="badge bg-secondary">Taslak</span>
                                @endif
                            </td>
                            <td style="text-align:center;font-size:13px;color:var(--text2)">{{ number_format($ep->view_count) }}</td>
                            <td style="text-align:right;padding-right:20px">
                                <a href="{{ route('admin.episodes.edit', $ep) }}" class="btn btn-sm btn-secondary">
                                    <i class="bi bi-pencil"></i>
                                </a>
                            </td>
                        </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
            @else
            <div class="empty-state py-4">
                <i class="bi bi-collection-play"></i>
                <p>Henüz bölüm yok.</p>
                <a href="{{ route('admin.episodes.create', ['anime_id'=>$anime->id,'season_id'=>$season->id]) }}"
                   class="btn btn-sm btn-success mt-1"><i class="bi bi-plus-lg"></i> Bölüm Ekle</a>
            </div>
            @endif
        </div>
        @empty
        <div class="card">
            <div class="card-body">
                <div class="empty-state">
                    <i class="bi bi-layers"></i>
                    <h6>Henüz sezon eklenmemiş</h6>
                    <button class="btn btn-primary btn-sm mt-2" data-bs-toggle="modal" data-bs-target="#addSeasonModal">
                        <i class="bi bi-plus-circle"></i> İlk Sezonu Ekle
                    </button>
                </div>
            </div>
        </div>
        @endforelse

        {{-- İzin Ayarları --}}
        @if(isset($permissions) && count($permissions))
        <div class="card">
            <div class="card-header">
                <i class="bi bi-shield-lock" style="color:var(--success)"></i>
                <h6>Bu Anime İçin İzin Ayarları</h6>
                <span style="font-size:12px;color:var(--text2)">— Global ayarları override eder</span>
            </div>
            <div class="card-body p-0">
                <form method="POST" action="{{ route('admin.animes.permissions', $anime) }}">
                    @csrf
                    @foreach($permissions as $perm)
                    @php $current = $contentPerms[$perm->key] ?? 'free'; @endphp
                    <div class="perm-row">
                        <div>
                            <div class="perm-name">{{ $perm->label }}</div>
                            @if($perm->description)<div class="perm-desc">{{ $perm->description }}</div>@endif
                        </div>
                        <div class="perm-toggle">
                            <input type="radio" name="permissions[{{ $perm->key }}]" id="ap_{{ $perm->key }}_free" value="free" {{ $current==='free'?'checked':'' }}>
                            <label for="ap_{{ $perm->key }}_free" class="free">Ücretsiz</label>
                            <input type="radio" name="permissions[{{ $perm->key }}]" id="ap_{{ $perm->key }}_prem" value="premium" {{ $current==='premium'?'checked':'' }}>
                            <label for="ap_{{ $perm->key }}_prem" class="premium">Premium</label>
                        </div>
                    </div>
                    @endforeach
                    <div class="p-3">
                        <button type="submit" class="btn btn-primary btn-sm">
                            <i class="bi bi-floppy"></i> İzinleri Kaydet
                        </button>
                    </div>
                </form>
            </div>
        </div>
        @endif
    </div>

    {{-- ── Sağ: Anime Bilgileri ── --}}
    <div class="col-lg-4">
        <div class="card mb-3">
            <div class="card-body text-center" style="padding:24px">
                @if($anime->coverUrl)
                    <img src="{{ $anime->coverUrl }}" alt="{{ $anime->title }}"
                         style="width:100%;max-width:180px;aspect-ratio:2/3;object-fit:cover;border-radius:var(--radius-sm);margin-bottom:16px">
                @endif
                <div style="font-size:16px;font-weight:800;color:var(--text);margin-bottom:4px">{{ $anime->title }}</div>
                @if($anime->title_en)
                    <div style="font-size:12.5px;color:var(--text2);margin-bottom:12px">{{ $anime->title_en }}</div>
                @endif
                <div class="d-flex justify-content-center gap-2 flex-wrap">
                    <span class="badge bg-secondary">{{ strtoupper($anime->type) }}</span>
                    <span class="badge {{ $anime->is_published?'bg-success':'bg-secondary' }}">
                        {{ $anime->is_published?'Yayında':'Taslak' }}
                    </span>
                    @if($anime->is_featured)
                        <span class="badge bg-warning" style="color:#000">Öne Çıkan</span>
                    @endif
                </div>
            </div>
        </div>

        <div class="card mb-3">
            <div class="card-header">
                <i class="bi bi-info-circle" style="color:var(--accent-lt)"></i>
                <h6>Detaylar</h6>
            </div>
            <div class="card-body p-0">
                @foreach([
                    ['Durum', $anime->status, ''],
                    ['Yayın Yılı', $anime->release_year ?: '—', ''],
                    ['Stüdyo', $anime->studio ?: '—', ''],
                    ['Puan', $anime->rating ?: '—', ''],
                    ['MAL ID', $anime->mal_id ?: '—', ''],
                    ['Türler', $anime->genres->pluck('name')->join(', ') ?: '—', ''],
                ] as [$lbl, $val])
                <div style="display:flex;justify-content:space-between;align-items:center;padding:10px 16px;border-bottom:1px solid var(--border)">
                    <span style="font-size:12.5px;color:var(--text2)">{{ $lbl }}</span>
                    <span style="font-size:13px;font-weight:600;color:var(--text);text-align:right;max-width:60%">{{ $val }}</span>
                </div>
                @endforeach
            </div>
        </div>

        <div class="d-flex flex-column gap-2">
            <a href="{{ route('admin.animes.edit', $anime) }}" class="btn btn-primary">
                <i class="bi bi-pencil"></i> Düzenle
            </a>
            <a href="{{ route('admin.episodes.index', ['anime_id'=>$anime->id]) }}" class="btn btn-secondary">
                <i class="bi bi-collection-play"></i> Tüm Bölümleri Gör
            </a>
        </div>
    </div>
</div>

{{-- Sezon Ekle Modal --}}
<div class="modal fade" id="addSeasonModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h6 class="modal-title"><i class="bi bi-plus-circle me-2" style="color:var(--accent-lt)"></i>Sezon Ekle</h6>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <form method="POST" action="{{ route('admin.animes.seasons.store', $anime) }}">
                @csrf
                <div class="modal-body">
                    <div class="mb-3">
                        <label class="form-label">Sezon Numarası <span style="color:var(--danger)">*</span></label>
                        <input type="number" name="season_number" class="form-control"
                               value="{{ $anime->seasons->count() + 1 }}" min="1" required>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Başlık <span style="color:var(--text2);font-size:11px">(opsiyonel)</span></label>
                        <input type="text" name="title" class="form-control" placeholder="1. Sezon">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Yayın Yılı</label>
                        <input type="number" name="release_year" class="form-control" value="{{ date('Y') }}">
                    </div>
                    <div class="d-flex align-items-center justify-content-between p-3 rounded" style="background:var(--surface2);border:1px solid var(--border)">
                        <div style="font-size:13.5px;font-weight:600;color:var(--text)">Yayınla</div>
                        <div class="form-check form-switch mb-0">
                            <input class="form-check-input" type="checkbox" name="is_published" value="1" id="sp" role="switch" checked>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">İptal</button>
                    <button type="submit" class="btn btn-primary"><i class="bi bi-plus-circle"></i> Sezon Ekle</button>
                </div>
            </form>
        </div>
    </div>
</div>

@endsection
