<?php

namespace App\Http\Controllers;

use App\Helpers\XenForoHelpers;
use App\Models\Entry;
use App\Models\News;
use App\Services\XenforoService;
use Artesaos\SEOTools\Facades\SEOTools;
use Illuminate\Http\Request;
class QueueController extends Controller
{
    public function index()
    {
        $entries = Entry::inQueue()
            ->with(['authors', 'game.platform'])
            ->orderByRaw("CASE WHEN state = 'pending' THEN 1 ELSE 0 END")
            ->orderBy('created_at', 'asc')
            ->get()
            ->map(fn($item) => $item->setAttribute('queue_type', 'entry'));

        $news = News::inQueue()
            ->orderByRaw("CASE WHEN state = 'pending' THEN 1 ELSE 0 END")
            ->orderBy('created_at', 'asc')
            ->get()
            ->map(fn($item) => $item->setAttribute('queue_type', 'news'));

        $entries = $entries->concat($news)->sort(function($a, $b) {
            $aPending = $a->state === 'pending' ? 0 : 1;
            $bPending = $b->state === 'pending' ? 0 : 1;

            if($aPending !== $bPending) {
                return $aPending <=> $bPending;
            }

            return $a->created_at <=> $b->created_at;
        })->values();

        SEOTools::setTitle('Submissions Queue');
        return view('queue.index', compact('entries'));
    }

    public function updateComment(Request $request, Entry $entry)
    {
        $request->validate(['comment' => 'nullable|string|max:2000']);

        $entry->update(['staff_comment' => $request->input('comment')]);

        return back()->with('success', 'Comment supdated');
    }

    public function updateComment_news(Request $request, News $news)
    {
        $request->validate(['comment' => 'nullable|string|max:2000']);

        $news->update(['staff_comment' => $request->input('comment')]);

        return back()->with('success', 'Comment updated');
    }

    public function approve(Request $request, Entry $entry)
    {
        $entry->update(['state' => 'published', 'created_at' => now()]);

        XenForoHelpers::entryApproved($entry);

        return back()->with('success', 'Entry approved');
    }

    public function approve_news(Request $request, News $news)
    {
        $news->update(['state' => 'published', 'created_at' => now()]);

        XenForoHelpers::entryApproved($news);

        return back()->with('success', 'Entry approved');
    }

    public function reject(Request $request, Entry $entry)
    {
        $request->validate(['reason' => 'nullable|string|max:2000']);

        $entry->update(['state' => 'rejected', 'staff_comment' => $request->input('reason'), 'rejected_at' => now() ]);

        XenForoHelpers::entryRejected($entry);
        return back()->with('success', 'Entry rejected');
    }

    public function reject_news(Request $request, News $news)
    {
        $request->validate(['reason' => 'nullable|string|max:2000']);

        $news->update(['state' => 'rejected', 'staff_comment' => $request->input('reason'), 'rejected_at' => now() ]);

        XenForoHelpers::entryRejected($news);
        return back()->with('success', 'Entry rejected');
    }

}
