<?php

declare(strict_types=1);

return static function (string $tableSuffix): callable {
    return static function (PDO $pdo): void {
        $driver = (string) $pdo->getAttribute(PDO::ATTR_DRIVER_NAME);
        $hasColumn = static function (string $table, string $column) use ($pdo, $driver): bool {
            if ($driver === 'sqlite') {
                $statement = $pdo->query('PRAGMA table_info(' . $table . ')');
                foreach (($statement ? $statement->fetchAll(PDO::FETCH_ASSOC) : []) as $row) {
                    if ((string) ($row['name'] ?? '') === $column) {
                        return true;
                    }
                }
                return false;
            }
            $statement = $pdo->prepare('SELECT COUNT(*) FROM information_schema.columns WHERE table_schema = DATABASE() AND table_name = :table AND column_name = :column');
            $statement->execute([':table' => $table, ':column' => $column]);
            return (int) $statement->fetchColumn() > 0;
        };
        $addColumn = static function (string $column) use ($pdo, $hasColumn): void {
            if (!$hasColumn('site_settings', $column)) {
                $pdo->exec('ALTER TABLE site_settings ADD COLUMN ' . $column . ' VARCHAR(80) DEFAULT NULL');
            }
        };

        $addColumn('creative_nav_style');
        $addColumn('creative_button_style');
    };
};
