<?php

use App\Http\Resources\PersonCollection;
use App\Models\Person\Person;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

Route::get('/user', function (Request $request) {
    return $request->user();
})->middleware('auth:sanctum');

Route::get('/person', function () {
    return new PersonCollection(Person::all());
})->middleware('auth:sanctum');

Route::get('/search', function (Request $request) {
    $query = '41242523';

    $persons = App\Models\Person\Person::search($query)->get();

    return $persons;
})->middleware('auth:sanctum');

// Import entities and suggestions
Route::get('/import-entities', [\App\Http\Controllers\ImportEntityController::class, 'index'])->name('api.importEntities.index');
Route::post('/import-entities/suggest', [\App\Http\Controllers\ImportEntityController::class, 'suggest'])->name('api.importEntities.suggest');

// Actions with decisions for document template settings UI
Route::get('/actions-with-decisions', function () {
    $actions = \App\Models\Action::with(['decisions:id,name'])->orderBy('name')->get(['id', 'name']);

    return [
        'actions' => $actions->map(fn ($a) => [
            'id' => $a->id,
            'name' => $a->name,
            'decisions' => $a->decisions->map(fn ($d) => ['id' => $d->id, 'name' => $d->name])->values(),
        ]),
    ];
})->middleware('auth:sanctum');
