<?php

declare(strict_types=1);

/**
 * M7.2 Aggregation layer: daily property aggregates.
 *
 * @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_property_agg`';
    $pdo->exec(
        "CREATE TABLE IF NOT EXISTS {$t} (
            `id` bigint unsigned NOT NULL AUTO_INCREMENT,
            `property_id` bigint unsigned NOT NULL,
            `site_url` varchar(2048) NOT NULL,
            `date` date 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_property_date` (`property_id`, `date`),
            KEY `idx_property_date` (`property_id`, `date`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci"
    );
};

