<?php

declare(strict_types=1);

/**
 * Speed up keywords search/filter when device+country dimensions exist.
 *
 * @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`';

    // Supports WHERE property_id + device + country + date range, and prefix LIKE on query.
    // (range on date / query can limit index usage, but this composite index significantly reduces scanned rows).
    try { $pdo->exec("ALTER TABLE {$t} ADD KEY `idx_prop_dev_ctr_date_query` (`property_id`, `device`, `country`, `date`, `query`(191))"); } catch (\Throwable) {}
};

