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

try {
    $pdo = db();
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    // Hashed password for 'password123'
    $hashed_password = password_hash('password123', PASSWORD_DEFAULT);

    // --- Company 1 & User 1 ---
    $stmt = $pdo->prepare("INSERT INTO companies (name) VALUES (?)");
    $stmt->execute(['Test Company Alpha']);
    $company1_id = $pdo->lastInsertId();

    $stmt = $pdo->prepare("INSERT INTO users (company_id, email, password, role) VALUES (?, ?, ?, ?)");
    $stmt->execute([$company1_id, 'user.alpha@example.com', $hashed_password, 'admin']);
    $user1_id = $pdo->lastInsertId();

    // --- Company 2 & User 2 ---
    $stmt = $pdo->prepare("INSERT INTO companies (name) VALUES (?)");
    $stmt->execute(['Test Company Beta']);
    $company2_id = $pdo->lastInsertId();

    $stmt = $pdo->prepare("INSERT INTO users (company_id, email, password, role) VALUES (?, ?, ?, ?)");
    $stmt->execute([$company2_id, 'user.beta@example.com', $hashed_password, 'admin']);
    $user2_id = $pdo->lastInsertId();
    
    // --- User 3 (in Company 2, not admin) ---
    $stmt = $pdo->prepare("INSERT INTO users (company_id, email, password, role) VALUES (?, ?, ?, ?)");
    $stmt->execute([$company2_id, 'employee.beta@example.com', $hashed_password, 'employee']);
    $user3_id = $pdo->lastInsertId();

    echo "Successfully created 2 companies and 3 test users.\n";
    echo "You can log in with:\n";
    echo "- user.alpha@example.com (password: password123)\n";
    echo "- user.beta@example.com (password: password123)\n";
    echo "- employee.beta@example.com (password: password123)\n";

} catch (PDOException $e) {
    die("Database seeding failed: " . $e->getMessage());
}
