<?php
if (session_status() == PHP_SESSION_NONE) {
    session_start();
}
require_once __DIR__ . '/../includes/db.php';
require_once __DIR__ . '/../includes/auth.php';
require_once __DIR__ . '/../includes/helpers.php';

check_role('administrador');

$user = get_logged_in_user();

$user_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;

if (!$user_id) {
    header('Location: users.php');
    exit;
}

// Get user details
$stmt = $pdo->prepare("SELECT u.*, r.name as role_name FROM users u JOIN roles r ON u.role_id = r.id WHERE u.id = ?");
$stmt->execute([$user_id]);
$user_data = $stmt->fetch();

if (!$user_data) {
    header('Location: users.php');
    exit;
}

// Get role-specific information
$role_info = [];
switch ($user_data['role_name']) {
    case 'empresa':
        $stmt = $pdo->prepare("SELECT * FROM companies WHERE user_id = ?");
        $stmt->execute([$user_id]);
        $role_info = $stmt->fetch();
        break;
    case 'alumno':
        $stmt = $pdo->prepare("SELECT s.*, c.name as career_name FROM students s LEFT JOIN careers c ON s.career_id = c.id WHERE s.user_id = ?");
        $stmt->execute([$user_id]);
        $role_info = $stmt->fetch();
        break;
    case 'profesor':
        $stmt = $pdo->prepare("SELECT t.*, c.name as career_name FROM teachers t LEFT JOIN careers c ON t.career_id = c.id WHERE t.user_id = ?");
        $stmt->execute([$user_id]);
        $role_info = $stmt->fetch();
        break;
    case 'egresado':
        $stmt = $pdo->prepare("SELECT g.*, c.name as career_name FROM graduates g LEFT JOIN careers c ON g.career_id = c.id WHERE g.user_id = ?");
        $stmt->execute([$user_id]);
        $role_info = $stmt->fetch();
        break;
}

$page_title = 'Detalles del Usuario - ' . htmlspecialchars($user_data['first_name'] . ' ' . $user_data['last_name']);

$content = '
<div class="row">
    <div class="col-md-4">
        <div class="card card-primary card-outline">
            <div class="card-body box-profile">
                <div class="text-center">
                    <img class="profile-user-img img-fluid img-circle" src="' . ((isset($user_data['photo']) && !empty($user_data['photo']) && file_exists(BASE_PATH . '/' . $user_data['photo'])) ? url($user_data['photo']) : url('assets/img/avatar.png')) . '" alt="User profile picture" style="width: 100px; height: 100px; object-fit: cover;">
                </div>
                <h3 class="profile-username text-center">' . htmlspecialchars($user_data['first_name'] . ' ' . $user_data['last_name']) . '</h3>
                <p class="text-muted text-center">' . ucfirst($user_data['role_name']) . '</p>
                <ul class="list-group list-group-unbordered mb-3">
                    <li class="list-group-item">
                        <b>Username</b> <a class="float-right">' . htmlspecialchars($user_data['username']) . '</a>
                    </li>
                    <li class="list-group-item">
                        <b>Email</b> <a class="float-right">' . htmlspecialchars($user_data['email']) . '</a>
                    </li>
                    <li class="list-group-item">
                        <b>Registro</b> <a class="float-right">' . date('d/m/Y', strtotime($user_data['created_at'])) . '</a>
                    </li>
                </ul>
                <div class="text-center">
                    <a href="edit-user.php?id=' . $user_id . '" class="btn btn-primary btn-sm"><i class="fas fa-edit"></i> Editar</a>
                    <a href="' . route_url('admin_delete_user', ['id' => $user_id]) . '" class="btn btn-danger btn-sm" onclick="return confirm(\'Estas seguro de que deseas eliminar este usuario?\')"><i class="fas fa-trash"></i> Borrar</a>
                </div>
            </div>
        </div>
    </div>
    <div class="col-md-8">
        <div class="card">
            <div class="card-header">
                <h3 class="card-title">Información del Usuario</h3>
            </div>
            <div class="card-body">
                <div class="row">
                    <div class="col-sm-6">
                        <strong><i class="fas fa-user mr-1"></i> Nombre completo</strong>
                        <p class="text-muted">' . htmlspecialchars($user_data['first_name'] . ' ' . $user_data['last_name']) . '</p>
                        <hr>
                        <strong><i class="fas fa-id-card mr-1"></i> DNI</strong>
                        <p class="text-muted">' . htmlspecialchars(isset($role_info['dni']) ? $role_info['dni'] : 'Not specified') . '</p>
                        <hr>
                        <strong><i class="fas fa-calendar-alt mr-1"></i> Fecha de Nacimiento</strong>
                        <p class="text-muted">' . (isset($role_info['birth_date']) && $role_info['birth_date'] ? date('d/m/Y', strtotime($role_info['birth_date'])) : 'No especificada') . '</p>
                        <hr>
                        <strong><i class="fas fa-phone mr-1"></i> Teléfono</strong>
                        <p class="text-muted">' . htmlspecialchars(isset($role_info['phone']) ? $role_info['phone'] : 'Not specified') . '</p>
                        <hr>
                        <strong><i class="fas fa-map-marker-alt mr-1"></i> Dirección</strong>
                        <p class="text-muted">' . htmlspecialchars(isset($role_info['address']) ? $role_info['address'] : 'Not specified') . '</p>
                        <hr>
                        <strong><i class="fas fa-envelope mr-1"></i> Email</strong>
                        <p class="text-muted">' . htmlspecialchars($user_data['email']) . '</p>
                        <hr>
                        <strong><i class="fas fa-user-tag mr-1"></i> Role</strong>
                        <p class="text-muted">' . ucfirst($user_data['role_name']) . '</p>
                        <hr>
                        <strong><i class="fas fa-calendar mr-1"></i> Fecha de registro</strong>
                        <p class="text-muted">' . date('d/m/Y', strtotime($user_data['created_at'])) . '</p>
                    </div>
                    <div class="col-sm-6">';

// Add role-specific information
switch ($user_data['role_name']) {
    case 'empresa':
        if ($role_info) {
            $content .= '<strong><i class="fas fa-building mr-1"></i> Razon Social</strong>
                        <p class="text-muted">' . htmlspecialchars($role_info['company_name'] ?: 'Not specified') . '</p>
                        <hr>
                        <strong><i class="fas fa-globe mr-1"></i> Website</strong>
                        <p class="text-muted">' . ($role_info['website'] ? '<a href="' . htmlspecialchars($role_info['website']) . '" target="_blank">' . htmlspecialchars($role_info['website']) . '</a>' : 'Not specified') . '</p>
                        <hr>';
        }
        break;
    case 'alumno':
        if ($role_info) {
            $content .= '<strong><i class="fas fa-graduation-cap mr-1"></i> Programa de Estudio</strong>
                         <p class="text-muted">' . htmlspecialchars($role_info['career_name'] ?: 'Not specified') . '</p>
                         <hr>
                         <strong><i class="fas fa-calendar-alt mr-1"></i> Año de Egreso</strong>
                         <p class="text-muted">' . htmlspecialchars($role_info['graduation_year'] ?: 'Not specified') . '</p>
                         <hr>
                         <strong><i class="fas fa-file-alt mr-1"></i> Resumen Profesional</strong>
                         <p class="text-muted">' . (isset($role_info['resume']) && $role_info['resume'] ? nl2br(htmlspecialchars($role_info['resume'])) : 'No especificado') . '</p>
                         <hr>
                         <div class="text-center">
                             <a href="resume.php?id=' . $user_id . '" class="btn btn-info btn-sm"><i class="fas fa-file-user"></i> Ver CV Completo</a>
                         </div>';
        }
        break;
    case 'profesor':
        if ($role_info) {
            $content .= '<strong><i class="fas fa-chalkboard-teacher mr-1"></i> Programa</strong>
                         <p class="text-muted">' . htmlspecialchars($role_info['career_name'] ?: 'Not specified') . '</p>
                         <hr>
                         <strong><i class="fas fa-info-circle mr-1"></i> Bio</strong>
                         <p class="text-muted">' . htmlspecialchars($role_info['bio'] ?: 'Not specified') . '</p>';
            $content .= '<hr>
                         ';
        }
        break;
    case 'egresado':
        if ($role_info) {
            $content .= '<strong><i class="fas fa-graduation-cap mr-1"></i> Programa de Estudio</strong>
                         <p class="text-muted">' . htmlspecialchars($role_info['career_name'] ?: 'Not specified') . '</p>
                         <hr>
                         <strong><i class="fas fa-calendar-alt mr-1"></i> Año de Egreso</strong>
                         <p class="text-muted">' . htmlspecialchars($role_info['graduation_year'] ?: 'Not specified') . '</p>
                         <hr>
                         <strong><i class="fas fa-file-alt mr-1"></i> Resumen Profesional</strong>
                         <p class="text-muted">' . (isset($role_info['resume']) && $role_info['resume'] ? nl2br(htmlspecialchars($role_info['resume'])) : 'No especificado') . '</p>
                         <hr>
                         <div class="text-center">
                             <a href="resume.php?id=' . $user_id . '" class="btn btn-info btn-sm"><i class="fas fa-file-user"></i> Ver CV Completo</a>
                         </div>';
        }
        break;
}

$content .= '</div>
                </div>
            </div>
        </div>
    </div>
</div>';

include '../includes/layout.php';
?>