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

echo "<h1>Setting up Contact Group Tables...</h1>";

try {
    // Contact Groups Table
    $sql1 = "CREATE TABLE IF NOT EXISTS `contact_groups` (
      `id` INT AUTO_INCREMENT PRIMARY KEY,
      `name` VARCHAR(100) NOT NULL,
      `owner_type` ENUM('admin', 'user') NOT NULL DEFAULT 'user',
      `owner_id` INT NOT NULL,
      `status` ENUM('active', 'inactive') DEFAULT 'active',
      `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;";

    $pdo->exec($sql1);
    echo "Table 'contact_groups' created or already exists.<br>";

    // Contact Group Members Table
    $sql2 = "CREATE TABLE IF NOT EXISTS `contact_group_members` (
      `id` INT AUTO_INCREMENT PRIMARY KEY,
      `group_id` INT NOT NULL,
      `phone_number` VARCHAR(20) NOT NULL,
      FOREIGN KEY (`group_id`) REFERENCES `contact_groups`(`id`) ON DELETE CASCADE
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;";

    $pdo->exec($sql2);
    echo "Table 'contact_group_members' created or already exists.<br>";

    echo "<h3 style='color:green'>Database Setup Complete!</h3>";

} catch (PDOException $e) {
    echo "<h3 style='color:red'>Error: " . $e->getMessage() . "</h3>";
}
?>