<?php

declare(strict_types=1);

/**
 * Shared server cache rows: deferred dashboard widgets and SEO AI Assistant JSON (see SeoAiAssistantDashboardCacheEnvelope).
 *
 * @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 . 'dashboard_cache`';

    $pdo->exec(
        "CREATE TABLE IF NOT EXISTS {$t} (
            `id` bigint unsigned NOT NULL AUTO_INCREMENT,
            `user_id` int unsigned NOT NULL,
            `property_id` bigint unsigned NOT NULL,
            `preset` varchar(32) NOT NULL,
            `group_key` varchar(32) NOT NULL,
            `computed_for_end` date NOT NULL,
            `payload_json` longtext NOT NULL,
            `computed_at` datetime NOT NULL,
            PRIMARY KEY (`id`),
            UNIQUE KEY `uniq_cache_key` (`user_id`, `property_id`, `preset`, `group_key`, `computed_for_end`),
            KEY `idx_user_prop_end` (`user_id`, `property_id`, `computed_for_end`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci"
    );
};

