<?php

namespace App\Http\Controllers\Admin;

use App\Actions\Records\ModerateRecordAction;
use App\Actions\Videos\ModerateVideoAction;
use App\Enums\ModerationStatus;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\ModerateContentRequest;
use App\Models\PlayerRecord;
use App\Models\PlayerVideo;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;

class ModerationController extends Controller
{
    public function index(): View
    {
        $this->authorize('moderate-records');

        return view('admin.moderation.index', [
            'records' => PlayerRecord::query()
                ->with(['game', 'playerProfile.user'])
                ->where('moderation_status', ModerationStatus::Pending)
                ->latest()
                ->paginate(20, ['*'], 'records_page'),
            'videos' => PlayerVideo::query()
                ->with(['game', 'playerProfile.user'])
                ->where('moderation_status', ModerationStatus::Pending)
                ->latest()
                ->paginate(20, ['*'], 'videos_page'),
        ]);
    }

    public function moderateRecord(
        ModerateContentRequest $request,
        PlayerRecord $record,
        ModerateRecordAction $action,
    ): RedirectResponse {
        $this->authorize('moderate-records');

        $action->execute(
            $record,
            auth()->user(),
            ModerationStatus::from($request->validated('status')),
            $request->validated('reason'),
        );

        return back()->with('status', __('admin.moderation.approved_success'));
    }

    public function moderateVideo(
        ModerateContentRequest $request,
        PlayerVideo $video,
        ModerateVideoAction $action,
    ): RedirectResponse {
        $this->authorize('moderate-videos');

        $action->execute(
            $video,
            auth()->user(),
            ModerationStatus::from($request->validated('status')),
            $request->validated('reason'),
        );

        return back()->with('status', __('admin.moderation.approved_success'));
    }
}
