<?php

declare(strict_types=1);

namespace App\Repositories;

use App\Models\DemoData;

final class InstitutionRepository extends BaseRepository
{
    public function all(): array
    {
        if (!$this->pdo) {
            return DemoData::institutions();
        }

        $statement = $this->pdo->query('SELECT * FROM institutions ORDER BY name ASC');

        return $statement->fetchAll();
    }

    public function recommendedFor(string $category, string $municipality = 'Пловдив'): array
    {
        $institutions = $this->all();
        $category = \u_lower($category);

        foreach ($institutions as $institution) {
            $responsibilities = \u_lower((string) $institution['responsibilities']);

            if (
                str_contains($category, 'замърсяване') && str_contains($responsibilities, 'замърсяване')
                || str_contains($category, 'потребител') && str_contains($responsibilities, 'потребител')
                || str_contains($category, 'път') && str_contains($responsibilities, 'пътища')
            ) {
                return $institution;
            }
        }

        return $institutions[0];
    }
}
