<?php

namespace App\Http\Controllers;

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

class CallbackRequestController extends Controller
{
    use ResolvesCurrentOrganization;

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

        return view('callback-requests.index', [
            'organization' => $organization,
            'callbackRequests' => $organization->callbackRequests()
                ->with(['contact', 'conversation', 'assignedUser', 'completedBy'])
                ->orderByRaw("case when status in ('needs_phone', 'requested') then 0 else 1 end")
                ->latest('requested_at')
                ->get(),
        ]);
    }
}
