<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Anime;
use App\Models\Season;
use Illuminate\Http\Request;

class SeasonController extends Controller
{
    public function index(Anime $anime) { return redirect()->route('admin.animes.show', $anime); }
    public function create(Anime $anime) { return redirect()->route('admin.animes.show', $anime); }
    public function show(Season $season) { return redirect()->route('admin.animes.show', $season->anime_id); }

    public function store(Request $request, Anime $anime)
    {
        $data = $request->validate([
            'season_number' => 'required|integer|min:1',
            'title'         => 'nullable|string|max:255',
            'description'   => 'nullable|string',
            'release_year'  => 'nullable|integer|min:1900|max:2099',
            'is_published'  => 'boolean',
        ]);

        $data['anime_id'] = $anime->id;
        $data['is_published'] = $request->boolean('is_published');

        Season::create($data);
        return redirect()->route('admin.animes.show', $anime)->with('success', 'Sezon eklendi.');
    }

    public function edit(Season $season)
    {
        return view('admin.seasons.edit', compact('season'));
    }

    public function update(Request $request, Season $season)
    {
        $data = $request->validate([
            'season_number' => 'required|integer|min:1',
            'title'         => 'nullable|string|max:255',
            'description'   => 'nullable|string',
            'release_year'  => 'nullable|integer|min:1900|max:2099',
            'is_published'  => 'boolean',
        ]);

        $data['is_published'] = $request->boolean('is_published');
        $season->update($data);
        return redirect()->route('admin.animes.show', $season->anime_id)->with('success', 'Sezon güncellendi.');
    }

    public function destroy(Season $season)
    {
        $animeId = $season->anime_id;
        $season->delete();
        return redirect()->route('admin.animes.show', $animeId)->with('success', 'Sezon silindi.');
    }
}
