<?php
namespace Api\Controllers\V1;

use Api\Core\Request;
use Api\Core\Response;

class DipendentiController {
    private $dipendenti = [
        [ 'codiceFiscale' => 'ABCDEF12G34H567J', 'nominativo' => 'ROSSI Mario', 'email' => 'mario.rossi@unipr.it' ],
        [ 'codiceFiscale' => 'KLMNOP89Q01R234S', 'nominativo' => 'VERDI Giuseppe', 'email' => 'giuseppe.verdi@unipr.it' ],
        [ 'codiceFiscale' => 'TUVWXY56Z78A901B', 'nominativo' => 'BIANCHI Luca', 'email' => 'luca.bianchi@unipr.it' ],
        [ 'codiceFiscale' => 'CDEFGH23I45J678K', 'nominativo' => 'MAMMI Pier-Paolo', 'email' => 'pierpaolo.mammi@unipr.it' ],
    ];

    public function search(Request $req, Response $res) {
        $term = $req->query()['term'] ?? "";

        $filtered = array_filter($this->dipendenti, function($contratto) use ($term) {
            if (empty($term)) {
                return false;
            }
            $term = strtolower($term);
            return stripos($contratto['codiceFiscale'], $term) !== false || 
                   stripos($contratto['nominativo'], $term) !== false || 
                   stripos($contratto['email'], $term) !== false;
        });

        return $res->json(array_values($filtered));
    }
}
