<?php

declare(strict_types=1);

/**
 * M6 daily query facts from GSC Search Analytics.
 *
 * @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 . 'gsc_daily_query`';
    $pdo->exec(
        "CREATE TABLE {$t} (
            `id` bigint unsigned NOT NULL AUTO_INCREMENT,
            `site_url` varchar(2048) NOT NULL,
            `date` date NOT NULL,
            `query` varchar(1024) NOT NULL,
            `clicks` int unsigned NOT NULL DEFAULT 0,
            `impressions` int unsigned NOT NULL DEFAULT 0,
            `ctr` decimal(8,6) NOT NULL DEFAULT 0,
            `position` decimal(10,4) NOT NULL DEFAULT 0,
            `updated_at` datetime NOT NULL,
            PRIMARY KEY (`id`),
            UNIQUE KEY `uniq_site_date_query` (`site_url`(255), `date`, `query`(191)),
            KEY `idx_site_date` (`site_url`(255), `date`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci"
    );
};

