<?php
require 'backend/bd/Conexion.php';

echo "<h1>Agregando permisos para Tamizaje...</h1>";

// Columnas a agregar
$columns = [
    'view_salud_oral' => "ALTER TABLE user_permissions ADD COLUMN view_salud_oral TINYINT(1) DEFAULT 0",
    'edit_salud_oral' => "ALTER TABLE user_permissions ADD COLUMN edit_salud_oral TINYINT(1) DEFAULT 0",
    'view_salud_mental' => "ALTER TABLE user_permissions ADD COLUMN view_salud_mental TINYINT(1) DEFAULT 0",
    'edit_salud_mental' => "ALTER TABLE user_permissions ADD COLUMN edit_salud_mental TINYINT(1) DEFAULT 0"
];

foreach($columns as $columnName => $sql) {
    try {
        $connect->exec($sql);
        echo "<p style='color: green;'>✅ Columna $columnName agregada</p>";
    } catch(Exception $e) {
        if(strpos($e->getMessage(), 'Duplicate') !== false) {
            echo "<p style='color: orange;'>⚠️ Columna $columnName ya existe</p>";
        } else {
            echo "<p style='color: red;'>❌ Error: " . $e->getMessage() . "</p>";
        }
    }
}

echo "<h2>Columnas actuales:</h2>";
$result = $connect->query("SHOW COLUMNS FROM user_permissions");
echo "<ul>";
while($row = $result->fetch()) {
    echo "<li>{$row['Field']} - {$row['Type']}</li>";
}
echo "</ul>";

echo "<h2 style='color: green;'>✅ LISTO!</h2>";
echo "<a href='frontend/pacientes/historia.php?id=1' style='display: inline-block; padding: 10px 20px; background: blue; color: white; text-decoration: none; border-radius: 5px;'>Ver Historia</a>";
?>
