<?php

declare(strict_types=1);

/**
 * M1 initial schema: users table.
 *
 * @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');
    }
    $users = '`' . $prefix . 'users`';
    $pdo->exec(
        "CREATE TABLE {$users} (
            `id` int unsigned NOT NULL AUTO_INCREMENT,
            `email` varchar(255) NOT NULL,
            `password_hash` varchar(255) NOT NULL,
            `name` varchar(255) DEFAULT NULL,
            `role` varchar(32) NOT NULL DEFAULT 'admin',
            `created_at` datetime NOT NULL,
            `updated_at` datetime NOT NULL,
            PRIMARY KEY (`id`),
            UNIQUE KEY `email_unique` (`email`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci"
    );
};
