<?php

namespace App\Models;

use CodeIgniter\Model;

class RoleNavItemModel extends Model
{
    protected $table         = 'role_nav_items';
    protected $primaryKey    = 'id';
    protected $useTimestamps = true;

    // ✅ columns that actually exist now
    protected $allowedFields = ['role_id','nav_item_id'];

    /**
     * Accepts role names (strings from session), resolves to role IDs, then returns allowed nav_item_ids.
     */
    public function getNavItemIdsForRoles(array $roleNames): array
    {
        $roleNames = array_values(array_filter(array_map('strval', $roleNames)));
        if (empty($roleNames)) return [];

        $roleModel = new RoleModel();
        $roleIds   = $roleModel->getIdsByNames($roleNames);
        if (empty($roleIds)) return [];

        $rows = $this->select('nav_item_id')
            ->whereIn('role_id', $roleIds)
            ->findAll();

        return array_values(array_unique(array_column($rows, 'nav_item_id')));
    }
}
