<?php

declare(strict_types=1);

/**
 * M13 daily query+page 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_page`';
    $pdo->exec(
        "CREATE TABLE {$t} (
            `id` bigint unsigned NOT NULL AUTO_INCREMENT,
            `property_id` bigint unsigned NULL,
            `site_url` varchar(2048) NOT NULL,
            `date` date NOT NULL,
            `query` varchar(1024) NOT NULL,
            `page` varchar(2048) 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_prop_date_q_p` (`property_id`, `date`, `query`(191), `page`(191)),
            KEY `idx_prop_date` (`property_id`, `date`),
            KEY `idx_prop_query` (`property_id`, `query`(191)),
            KEY `idx_prop_page` (`property_id`, `page`(191))
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci"
    );
};

