<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\AnimeRequest;
use Illuminate\Http\Request;

class AnimeRequestController extends Controller
{
    public function index(Request $request)
    {
        $status = $request->input('status', 'pending');

        $requests = AnimeRequest::with('user:id,name,email')
            ->when($status !== 'all', fn($q) => $q->where('status', $status))
            ->orderByDesc('vote_count')
            ->orderByDesc('created_at')
            ->paginate(30);

        $counts = AnimeRequest::selectRaw('status, COUNT(*) as cnt')
            ->groupBy('status')
            ->pluck('cnt', 'status');

        return view('admin.anime-requests.index', compact('requests', 'counts', 'status'));
    }

    public function update(Request $request, AnimeRequest $animeRequest)
    {
        $data = $request->validate([
            'status'     => 'required|in:pending,approved,rejected,added',
            'admin_note' => 'nullable|string|max:500',
        ]);

        $animeRequest->update($data);

        return back()->with('success', 'İstek güncellendi.');
    }

    public function destroy(AnimeRequest $animeRequest)
    {
        $animeRequest->delete();
        return back()->with('success', 'İstek silindi.');
    }
}
