<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Genre;
use App\Services\ReceiverDatabaseSyncService;
use App\Services\TmdbService;
use Illuminate\Http\Request;
use Illuminate\Support\Str;

class GenresController extends Controller
{
    public function index(){ return view('admin.genres',['items'=>Genre::orderBy('name')->paginate(40)]); }
    public function store(Request $request){
        $data=$request->validate(['name'=>['required','string','max:100','unique:genres,name']]);
        Genre::create(['name'=>$data['name'],'slug'=>Str::slug($data['name']),'is_active'=>true]);
        return back()->with('ok','Genre added.');
    }
    public function sync(TmdbService $tmdb, ReceiverDatabaseSyncService $receiverSync){ foreach($tmdb->genres() as $list) foreach($list as $g) Genre::updateOrCreate(['tmdb_id'=>$g['id']],['name'=>$g['name'],'slug'=>Str::slug($g['name']),'is_active'=>true]); $receiverSync->syncAllGenres(); return back()->with('ok','TMDB genres synced to MovieDocks and Fojik.'); }
    public function toggle(Genre $genre){$genre->update(['is_active'=>!$genre->is_active]);return back();}
    public function destroy(Genre $genre){$genre->delete();return back();}
}
