<?php
/**
 * Script de Reparación Crítica - Todas las Tablas Operativas
 */
require_once 'includes/db.php';
error_reporting(E_ALL);
ini_set('display_errors', 1);

echo "<h1>Reparación Crítica de Tablas Operativas</h1>";

$tables = [
    'companies' => "CREATE TABLE companies (id INT AUTO_INCREMENT PRIMARY KEY, user_id INT NOT NULL UNIQUE, company_name VARCHAR(100) NOT NULL, description TEXT, website VARCHAR(255), address VARCHAR(255), phone VARCHAR(20), logo VARCHAR(255), created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE)",
    'careers' => "CREATE TABLE careers (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100) NOT NULL UNIQUE)",
    'jobs' => "CREATE TABLE jobs (id INT AUTO_INCREMENT PRIMARY KEY, company_id INT NOT NULL, career_id INT, title VARCHAR(255) NOT NULL, description TEXT NOT NULL, requirements TEXT, salary DECIMAL(10,2), location VARCHAR(100), job_type ENUM('full-time', 'part-time', 'contract', 'internship') DEFAULT 'full-time', status ENUM('active', 'inactive', 'filled') DEFAULT 'active', posted_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP, expiration_date DATE, view_count INT DEFAULT 0, FOREIGN KEY (company_id) REFERENCES companies(id) ON DELETE CASCADE)",
    'applications' => "CREATE TABLE applications (id INT AUTO_INCREMENT PRIMARY KEY, job_id INT NOT NULL, student_id INT NOT NULL, status ENUM('pending', 'reviewed', 'accepted', 'rejected') DEFAULT 'pending', applied_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP, notes TEXT, FOREIGN KEY (job_id) REFERENCES jobs(id) ON DELETE CASCADE, UNIQUE KEY unique_application (job_id, student_id))",
    'graduates' => "CREATE TABLE graduates (id INT AUTO_INCREMENT PRIMARY KEY, user_id INT NOT NULL UNIQUE, career_id INT, status ENUM('active', 'inactive') DEFAULT 'active', gender ENUM('masculino', 'femenino', 'femenino_masculino') DEFAULT 'masculino', created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE)",
    'students' => "CREATE TABLE students (id INT AUTO_INCREMENT PRIMARY KEY, user_id INT NOT NULL UNIQUE, career_id INT, gender ENUM('masculino', 'femenino', 'femenino_masculino') DEFAULT 'masculino', created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE)",
    'teachers' => "CREATE TABLE teachers (id INT AUTO_INCREMENT PRIMARY KEY, user_id INT NOT NULL UNIQUE, career_id INT, department VARCHAR(100), created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE)"
];

foreach ($tables as $name => $sql) {
    try {
        $pdo->exec($sql);
        echo "<p style='color:green'>✅ Tabla <strong>$name</strong> creada exitosamente.</p>";
    } catch (PDOException $e) {
        if (strpos($e->getMessage(), 'exists') !== false) {
            echo "<p style='color:blue'>✔ Tabla <strong>$name</strong> ya existe.</p>";
        } else {
            echo "<p style='color:red'>❌ Error en $name: " . $e->getMessage() . "</p>";
        }
    }
}

// Agregar columnas que faltan en tablas existentes (como view_count en jobs)
try {
    $pdo->exec("ALTER TABLE jobs ADD COLUMN view_count INT DEFAULT 0 AFTER status");
} catch (Exception $e) {}

try {
    $pdo->exec("ALTER TABLE students ADD COLUMN gender ENUM('masculino', 'femenino') NULL AFTER career_id");
} catch (Exception $e) {}

try {
    $pdo->exec("ALTER TABLE graduates ADD COLUMN gender ENUM('masculino', 'femenino') NULL AFTER career_id");
} catch (Exception $e) {}

echo "<h2>Finalizado. Prueba el Dashboard ahora.</h2>";
?>
