<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Concerns\ResolvesCurrentOrganization;
use Illuminate\Http\Request;
use Illuminate\View\View;

class UnknownQuestionController extends Controller
{
    use ResolvesCurrentOrganization;

    public function __invoke(Request $request): View
    {
        $organization = $this->currentOrganization($request);
        abort_if($organization === null, 404);
        abort_unless($this->currentUserCan($request, 'monitor_ai') || $this->currentUserCan($request, 'train_ai'), 403);

        return view('unknown-questions.index', [
            'organization' => $organization,
            'unknownQuestions' => $organization->unknownQuestions()
                ->with(['contact', 'conversation', 'resolvedBy'])
                ->orderByRaw("case when status = 'open' then 0 else 1 end")
                ->latest()
                ->get(),
            'reportedCorrections' => \App\Models\ReportedCorrection::where('organization_id', $organization->id)
                ->with(['contact', 'conversation', 'message', 'resolvedBy'])
                ->orderByRaw("case when status = 'pending' then 0 else 1 end")
                ->latest()
                ->get(),
            'intents' => $organization->intents()->orderBy('name')->get(),
        ]);
    }
}
