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

// 1. Create Table
$sql = "CREATE TABLE IF NOT EXISTS job_categories (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100) NOT NULL UNIQUE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;";

if ($conn->query($sql) === TRUE) {
    echo "Table job_categories created successfully.<br>";
} else {
    echo "Error creating table: " . $conn->error . "<br>";
}

// 2. Insert Existing Categories
$cats = get_job_categories();
foreach ($cats as $cat) {
    $cat = $conn->real_escape_string($cat);
    $check = $conn->query("SELECT id FROM job_categories WHERE name='$cat'");
    if ($check->num_rows == 0) {
        if ($conn->query("INSERT INTO job_categories (name) VALUES ('$cat')")) {
            echo "Inserted: $cat<br>";
        } else {
            echo "Error inserting $cat: " . $conn->error . "<br>";
        }
    } else {
        echo "Exists: $cat<br>";
    }
}

$conn->close();
?>
