<?php
// Script to simulate employer approval
include 'db_config.php';

// 1. Create a dummy pending employer
$email = 'test_employer_' . time() . '@example.com';
$password = password_hash('password123', PASSWORD_DEFAULT);
$company = 'Test Company ' . time();

$conn->query("INSERT INTO employer_login (username, password, role) VALUES ('$email', '$password', 'employer')");
$user_id = $conn->insert_id;
$conn->query("INSERT INTO employer_details (user_id, company_name, business_reg_no, contact_no, address, status) VALUES ($user_id, '$company', 'BR123', '0771234567', 'Colombo', 'Pending')");

echo "Created pending employer: $email (ID: $user_id)\n";

// 2. verify it is pending
$res = $conn->query("SELECT status FROM employer_details WHERE user_id=$user_id")->fetch_assoc();
if($res['status'] !== 'Pending') {
    echo "Error: Employer not created as Pending.\n";
    exit;
} else {
    echo "Employer status is Pending.\n";
}

// 3. Simulate Admin Approval (call the logic from approve_reject_employer.php)
// In a real browser test we would click the link, here we simulate the DB update
// mimic approve_reject_employer.php logic
$action = 'approve';
$status = ($action === 'approve') ? 'Approved' : 'Rejected';

$stmt = $conn->prepare("UPDATE employer_details SET status = ? WHERE user_id = ?");
$stmt->bind_param("si", $status, $user_id);
if ($stmt->execute()) {
    echo "Simulated Admin Approval executed.\n";
} else {
    echo "Error executing approval.\n";
}

// 4. Verify status is now Approved
$res = $conn->query("SELECT status FROM employer_details WHERE user_id=$user_id")->fetch_assoc();
if($res['status'] === 'Approved') {
    echo "Success: Employer status is now Approved.\n";
} else {
    echo "Error: Employer status is " . $res['status'] . "\n";
}

// Clean up
$stmt_del1 = $conn->prepare("DELETE FROM employer_login WHERE id=?");
$stmt_del1->bind_param("i", $user_id);
$stmt_del1->execute();
$stmt_del1->close();

$stmt_del2 = $conn->prepare("DELETE FROM employer_details WHERE user_id=?");
$stmt_del2->bind_param("i", $user_id);
$stmt_del2->execute();
$stmt_del2->close();
echo "Cleaned up test data.\n";

?>
