<?php

namespace App\Modules\Auth\Models;

use CodeIgniter\Model;

/**
 * User Role Model (pivot)
 */
class UserRoleModel extends Model
{
    protected $table            = 'user_roles';
    // Use a simple primary key to avoid composite PK issues in Model internals.
    // Database level tetap punya primary key (user_id, role_id) via migration.
    protected $primaryKey       = 'user_id';
    protected $useAutoIncrement = false;
    protected $returnType       = 'array';
    protected $useSoftDeletes   = false;
    protected $protectFields    = true;
    protected $allowedFields    = [
        'user_id',
        'role_id',
    ];

    protected $useTimestamps = false;

    /**
     * Get role IDs for a user
     *
     * @param int $userId
     * @return array<int>
     */
    public function getRoleIdsForUser(int $userId): array
    {
        $rows = $this->where('user_id', $userId)->findAll();
        $ids = [];
        foreach ($rows as $row) {
            $ids[] = (int) $row['role_id'];
        }
        return $ids;
    }
}
