<?php
require_once __DIR__ . '/config.php';

try {
    $pdo = db();

    // Create users table
    $pdo->exec("CREATE TABLE IF NOT EXISTS users (
        id INT AUTO_INCREMENT PRIMARY KEY,
        name VARCHAR(255) NOT NULL,
        email VARCHAR(255) NOT NULL UNIQUE,
        password VARCHAR(255) NOT NULL,
        role ENUM('admin', 'coach') NOT NULL DEFAULT 'coach',
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
    )");

    // Add sample users
    $users = [
        [
            'name' => 'Admin User',
            'email' => 'admin@example.com',
            'password' => password_hash('password', PASSWORD_DEFAULT),
            'role' => 'admin'
        ],
        [
            'name' => 'Coach User',
            'email' => 'coach@example.com',
            'password' => password_hash('password', PASSWORD_DEFAULT),
            'role' => 'coach'
        ]
    ];

    $stmt = $pdo->prepare("INSERT INTO users (name, email, password, role) VALUES (:name, :email, :password, :role)");

    foreach ($users as $user) {
        // Check if user already exists
        $check = $pdo->prepare("SELECT id FROM users WHERE email = :email");
        $check->execute(['email' => $user['email']]);
        if ($check->rowCount() == 0) {
            $stmt->execute($user);
        }
    }

    echo "Users table created and seeded successfully." . PHP_EOL;

} catch (PDOException $e) {
    die("DB ERROR: " . $e->getMessage());
}
