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

check_role('alumno');

$user = get_logged_in_user();

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

if (!$student) {
    die('Student profile not found.');
}

$page_title = 'Mis Postulaciones';

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

$stmt = $pdo->prepare("SELECT a.*, j.title, j.location, j.salary, c.company_name FROM applications a JOIN jobs j ON a.job_id = j.id JOIN companies c ON j.company_id = c.id WHERE a.applicant_id = ? AND a.applicant_type = 'student' ORDER BY a.applied_date DESC");
$stmt->execute([$student['id']]);

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

    $content .= '<tr>
        <td>' . htmlspecialchars($row['title']) . '</td>
        <td>' . htmlspecialchars($row['company_name']) . '</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>
    </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';
?>