<?php

declare(strict_types=1);

namespace BotGuard\Models;

use BotGuard\Core\Model;

/**
 * Per-user redirect destinations applied to a verification decision.
 */
final class RedirectConfig extends Model
{
    protected string $table = 'redirect_configs';

    /**
     * The default config for a user (falls back to any config, then null).
     *
     * @return array<string, mixed>|null
     */
    public function defaultForUser(int $userId): ?array
    {
        return $this->db->selectOne(
            "SELECT * FROM `{$this->table}` WHERE `user_id` = ? ORDER BY `is_default` DESC, `id` ASC LIMIT 1",
            [$userId]
        );
    }

    /**
     * The platform-wide default config (the seeded admin's default), used for
     * anonymous verification traffic in the single-tenant MVP.
     *
     * @return array<string, mixed>|null
     */
    public function platformDefault(): ?array
    {
        return $this->db->selectOne(
            "SELECT rc.* FROM `{$this->table}` rc
             JOIN `users` u ON u.id = rc.user_id
             WHERE u.role = 'admin'
             ORDER BY rc.is_default DESC, rc.id ASC LIMIT 1"
        );
    }
}
