<?php

declare(strict_types=1);

/**
 * Add initial range fields for job progress.
 *
 * @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 . 'sync_jobs`';
    try { $pdo->exec("ALTER TABLE {$t} ADD COLUMN `initial_start_date` date NULL AFTER `job_type`"); } catch (\Throwable) {}
    try { $pdo->exec("ALTER TABLE {$t} ADD COLUMN `initial_end_date` date NULL AFTER `initial_start_date`"); } catch (\Throwable) {}
    try { $pdo->exec("UPDATE {$t} SET initial_start_date = start_date WHERE initial_start_date IS NULL"); } catch (\Throwable) {}
    try { $pdo->exec("UPDATE {$t} SET initial_end_date = end_date WHERE initial_end_date IS NULL"); } catch (\Throwable) {}
    try { $pdo->exec("ALTER TABLE {$t} ADD KEY `idx_user_active` (`user_id`, `is_active`, `job_type`)"); } catch (\Throwable) {}
};

