<?php

declare(strict_types=1);

define('BASE_PATH', dirname(__DIR__));

$testDatabase = (string) getenv('PET_SPA_TEST_DB');
if (!preg_match('/^pet_shop_e2e_[a-z0-9_]+$/', $testDatabase)) {
    throw new RuntimeException('Refusing to seed: use an isolated pet_shop_e2e_* database.');
}

require_once BASE_PATH . '/config/database.php';

$database = new Database();
$pdo = $database->connect();
if ($database->databaseName() !== $testDatabase
    || $pdo->query('SELECT DATABASE()')->fetchColumn() !== $testDatabase
    || $testDatabase === 'pet_shop') {
    throw new RuntimeException('Refusing to seed the main database.');
}

$passwordHash = password_hash('password-e2e', PASSWORD_DEFAULT);
$stmt = $pdo->prepare(
    'INSERT INTO users (full_name, phone, email, password_hash, role, status)
     VALUES (:name, :phone, :email, :password_hash, :role, :status)'
);
foreach ([
    ['E2E Owner', '0812345678', 'owner-http@example.test', 'owner', 'active'],
    ['E2E Admin', '0823456789', 'admin-http@example.test', 'admin', 'active'],
    ['E2E Locked', '0834567890', 'locked-http@example.test', 'customer', 'locked']
] as [$name, $phone, $email, $role, $status]) {
    $stmt->execute([
        'name' => $name,
        'phone' => $phone,
        'email' => $email,
        'password_hash' => $passwordHash,
        'role' => $role,
        'status' => $status
    ]);
}

echo "HTTP E2E seed PASS\n";
