<?php
include 'db_config.php';
include 'functions.php';

// Simulate POST data
$_POST['company'] = 'Test Company ' . time();
$_POST['email'] = 'test' . time() . '@example.com';
$_POST['password'] = 'password123';
$_POST['phone'] = '0771234567';
$_POST['address'] = '123 Test St, Colombo';
$_POST['br_no'] = 'PV ' . time();

// Mock sanitize_input if not included properly or direct logic testing
function test_registration($conn) {
    echo "Testing Registration...\n";
    $email = $_POST['email'];
    $password = password_hash($_POST['password'], PASSWORD_DEFAULT);
    $company = $_POST['company'];
    $br_no = $_POST['br_no'];
    $phone = $_POST['phone'];
    $address = $_POST['address'];

    // Manually run the insert logic to verify database interaction (bypassing register.php HTML/Form complexity for CLI)
    
    if($conn->query("INSERT INTO employer_login (username, password, role) VALUES ('$email', '$password', 'employer')")) {
        $last_id = $conn->insert_id;
        $sql = "INSERT INTO employer_details (user_id, company_name, business_reg_no, contact_no, address, status) VALUES ($last_id, '$company', '$br_no', '$phone', '$address', 'Pending')";
        if($conn->query($sql)) {
            echo "SUCCESS: Employer registered.\n";
            
            // Verify Data
            $res = $conn->query("SELECT * FROM employer_details WHERE user_id=$last_id");
            $row = $res->fetch_assoc();
            echo "Verifying Business Reg No: " . $row['business_reg_no'] . "\n";
            if($row['business_reg_no'] === $br_no) {
                echo "PASSED: Business Reg No matches.\n";
            } else {
                echo "FAILED: Business Reg No mismatch.\n";
            }
            
            // Clean up
            $stmt_del1 = $conn->prepare("DELETE FROM employer_login WHERE id=?");
            $stmt_del1->bind_param("i", $last_id);
            $stmt_del1->execute();
            $stmt_del1->close();
            
            $stmt_del2 = $conn->prepare("DELETE FROM employer_details WHERE user_id=?");
            $stmt_del2->bind_param("i", $last_id);
            $stmt_del2->execute();
            $stmt_del2->close(); // Cascade should handle this but just in case
            echo "Cleaned up test data.\n";
            
        } else {
            echo "FAILED: Details Insert Error: " . $conn->error . "\n";
            echo "SQL: $sql\n";
        }
    } else {
        echo "FAILED: Login Insert Error: " . $conn->error . "\n";
    }
}

test_registration($conn);
?>
