<?php

declare(strict_types=1);

/**
 * Quick Win / drilldowns performance: composite indexes for gsc_daily_query_page.
 *
 * @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`';

    // These are safe to attempt even if already present (we swallow duplicates).
    $sqls = [
        "ALTER TABLE {$t} ADD KEY `idx_prop_dev_ctr_date_query` (`property_id`, `device`, `country`, `date`, `query`(191))",
        "ALTER TABLE {$t} ADD KEY `idx_prop_dev_ctr_date_page` (`property_id`, `device`, `country`, `date`, `page`(191))",
    ];

    foreach ($sqls as $sql) {
        try {
            $pdo->exec($sql);
        } catch (\Throwable) {
            // ignore (already exists / older MySQL limitations)
        }
    }
};

