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

check_role('profesor');

$user = get_logged_in_user();

// Get teacher
$stmt = $pdo->prepare("SELECT * FROM teachers WHERE user_id = ?");
$stmt->execute([$user['id']]);
$teacher = $stmt->fetch();

if (!$teacher) {
    die('Teacher profile not found.');
}

$page_title = 'Postulaciones de Estudiantes';

$content = '
<div class="row">
    <div class="col-12">
        <div class="card">
            <div class="card-header">
                <h3 class="card-title">Postulaciones de Estudiantes</h3>
            </div>
            <div class="card-body">
                <table class="table table-bordered table-striped">
                    <thead>
                        <tr>
                            <th>Estudiante</th>
                            <th>Cargo</th>
                            <th>Empresa</th>
                            <th>Programa</th>
                            <th>Lugar</th>
                            <th>Sueldo</th>
                            <th>Estado</th>
                            <th>Fecha de Postulación</th>
                            <th>Acciones</th>
                        </tr>
                    </thead>
                    <tbody>';

$stmt = $pdo->prepare("
    SELECT a.*, j.title, j.location, j.salary, c.company_name, u.first_name, u.last_name, car.name as career_name, a.applicant_type
    FROM applications a
    JOIN jobs j ON a.job_id = j.id
    JOIN companies c ON j.company_id = c.id
    LEFT JOIN students st ON a.applicant_id = st.id AND a.applicant_type = 'student'
    LEFT JOIN graduates g ON a.applicant_id = g.id AND a.applicant_type = 'graduate'
    JOIN users u ON (st.user_id = u.id OR g.user_id = u.id)
    LEFT JOIN careers car ON (st.career_id = car.id OR g.career_id = car.id)
    ORDER BY a.applied_date DESC
");
$stmt->execute();

while ($row = $stmt->fetch()) {
    $status_class = match($row['status']) {
        'pending' => 'badge-warning',
        'reviewed' => 'badge-info',
        'accepted' => 'badge-success',
        'rejected' => 'badge-danger',
        default => 'badge-secondary'
    };

    $applicant_type_label = match($row['applicant_type']) {
        'student' => ' (Alumno)',
        'graduate' => ' (Egresado)',
        default => ''
    };

    $content .= '<tr>
        <td>' . htmlspecialchars($row['first_name'] . ' ' . $row['last_name']) . '<small>' . $applicant_type_label . '</small></td>
        <td>' . htmlspecialchars($row['title']) . '</td>
        <td>' . htmlspecialchars($row['company_name']) . '</td>
        <td>' . htmlspecialchars($row['career_name'] ?: 'General') . '</td>
        <td>' . htmlspecialchars($row['location']) . '</td>
        <td>' . ($row['salary'] ? 'S/. ' . number_format($row['salary'], 2) : 'N/A') . '</td>
        <td><span class="badge ' . $status_class . '">' . match($row['status']) {
            'pending' => 'Pendiente',
            'reviewed' => 'Revisado',
            'accepted' => 'Aceptado',
            'rejected' => 'Rechazado',
            default => ucfirst($row['status'])
        } . '</span></td>
        <td>' . date('d/m/Y', strtotime($row['applied_date'])) . '</td>
        <td><a href="' . route_url('teacher_resume', ['id' => $row['user_id'] ?? 0]) . '" class="btn btn-sm btn-outline-primary"><i class="fas fa-eye"></i> Ver CV</a>
        <a href="' . route_url('teacher_job_detail', ['id' => $row['job_id']]) . '" class="btn btn-sm btn-outline-info ml-1"><i class="fas fa-briefcase"></i> Ver Oferta</a></td>
    </tr>';
}

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

$content .= '<style>
.badge {
    font-size: 0.75rem;
    padding: 0.5rem 0.75rem;
    border-radius: 20px;
    font-weight: 500;
    border: 1px solid rgba(255,255,255,0.3);
}
.badge-warning {
    background: #f39c12;
    color: white;
}
.badge-info {
    background: linear-gradient(135deg, #17a2b8, #138496);
    color: white;
}
.badge-success {
    background: linear-gradient(135deg, #28a745, #1e7e34);
    color: white;
}
.badge-danger {
    background: linear-gradient(135deg, #dc3545, #bd2130);
    color: white;
}
.badge-secondary {
    background: linear-gradient(135deg, #6c757d, #545b62);
    color: white;
}
</style>';

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