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

check_role('administrador');

$user = get_logged_in_user();

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

if (!$user_id) {
    header('Location: students.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 || $user_data['role_name'] !== 'alumno') {
    header('Location: students.php');
    exit;
}

// Get student-specific information
$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]);
$student_info = $stmt->fetch();

$page_title = 'Detalles del Alumno - ' . 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">Alumno</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="' . route_url('admin_students') . '" class="btn btn-secondary btn-sm"><i class="fas fa-arrow-left"></i> Volver</a>
                    
                </div>
            </div>
        </div>
    </div>
    <div class="col-md-8">
        <div class="card">
            <div class="card-header">
                <h3 class="card-title">Información del Alumno</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($student_info['dni'] ?: 'No especificado') . '</p>
                        <hr>
                        <strong><i class="fas fa-calendar-alt mr-1"></i> Fecha de Nacimiento</strong>
                        <p class="text-muted">' . ($student_info['birth_date'] ? date('d/m/Y', strtotime($student_info['birth_date'])) : 'No especificada') . '</p>
                        <hr>
                        <strong><i class="fas fa-phone mr-1"></i> Teléfono</strong>
                        <p class="text-muted">' . htmlspecialchars($student_info['phone'] ?: 'No especificado') . '</p>
                        <hr>
                        <strong><i class="fas fa-venus-mars mr-1"></i> Género</strong>
                        <p class="text-muted">' . htmlspecialchars($student_info['gender'] ?: 'No especificado') . '</p>
                        <hr>
                        <strong><i class="fas fa-map-marker-alt mr-1"></i> Dirección</strong>
                        <p class="text-muted">' . htmlspecialchars($student_info['address'] ?: 'No especificada') . '</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-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">
                        <strong><i class="fas fa-graduation-cap mr-1"></i> Programa de Estudio</strong>
                        <p class="text-muted">' . htmlspecialchars($student_info['career_name'] ?: 'No especificado') . '</p>
                        <hr>
                        <strong><i class="fas fa-calendar-alt mr-1"></i> Año de Graduación Esperado</strong>
                        <p class="text-muted">' . htmlspecialchars($student_info['graduation_year'] ?: 'No especificado') . '</p>
                        <hr>
                        <strong><i class="fas fa-file-alt mr-1"></i> Resumen Profesional</strong>
                        <p class="text-muted">' . ($student_info['resume'] ? nl2br(htmlspecialchars($student_info['resume'])) : 'No especificado') . '</p>
                        <hr>
                        
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>';

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