<?php

namespace App\Models\Control;

use CodeIgniter\Model;

/**
 * Control DB. Write-once audit trail for platform-operator actions — see
 * CreateAdminActivityLogTable. Mirrors App\Models\ActivityLogModel one
 * layer up (admin_users, not any tenant's users table).
 */
class AdminActivityLogModel extends Model
{
    protected $table         = 'admin_activity_log';
    protected $primaryKey    = 'id';
    protected $returnType    = 'object';
    protected $useTimestamps = true;
    protected $createdField  = 'created_at';
    protected $updatedField  = '';

    protected $allowedFields = [
        'admin_user_id',
        'module',
        'action',
        'created_at',
    ];

    public function log(?int $adminUserId, string $module, string $action): void
    {
        $this->insert([
            'admin_user_id' => $adminUserId,
            'module'        => $module,
            'action'        => $action,
        ]);
    }

    public function recent(int $limit = 50): array
    {
        return $this->orderBy('created_at', 'desc')->limit($limit)->findAll();
    }
}
