<?php

declare(strict_types=1);

/**
 * Per-user LLM token usage for SEO AI Assistant (OpenAI / Anthropic), for Settings UI and monthly caps.
 *
 * @return callable(PDO, string): void
 */
return static function (\PDO $pdo, string $prefix): void {
    if (!preg_match('/^[a-zA-Z0-9_]+$/', $prefix)) {
        throw new InvalidArgumentException('Invalid table prefix');
    }

    $t = '`' . $prefix . 'seo_ai_token_usage`';

    $pdo->exec(
        "CREATE TABLE IF NOT EXISTS {$t} (
            `id` bigint unsigned NOT NULL AUTO_INCREMENT,
            `user_id` int unsigned NOT NULL,
            `provider` varchar(16) NOT NULL,
            `input_tokens` int unsigned NOT NULL DEFAULT 0,
            `output_tokens` int unsigned NOT NULL DEFAULT 0,
            `total_tokens` int unsigned NOT NULL DEFAULT 0,
            `created_at` datetime NOT NULL,
            PRIMARY KEY (`id`),
            KEY `idx_user_provider_created` (`user_id`, `provider`, `created_at`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci"
    );
};
