<?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');

$job_id = $_GET['id'] ?? 0;
if (!$job_id) {
    header('Location: ' . route_url('admin_jobs'));
    exit;
}

// Get job data
$stmt = $pdo->prepare("SELECT j.*, c.company_name FROM jobs j JOIN companies c ON j.company_id = c.id WHERE j.id = ?");
$stmt->execute([$job_id]);
$job = $stmt->fetch();

if (!$job) {
    header('Location: ' . route_url('admin_jobs'));
    exit;
}

// Get application count
$stmt = $pdo->prepare("SELECT COUNT(*) as count FROM applications WHERE job_id = ?");
$stmt->execute([$job_id]);
$app_count = $stmt->fetch()['count'];

$message = '';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if (isset($_POST['confirm_delete'])) {
        try {
            // Start transaction
            $pdo->beginTransaction();

            // Soft delete job
            $stmt = $pdo->prepare("UPDATE jobs SET deleted_at = NOW() WHERE id = ?");
            $stmt->execute([$job_id]);

            $pdo->commit();
            header('Location: ' . route_url('admin_jobs', ['deleted' => '1']));
            exit;
        } catch (Exception $e) {
            $pdo->rollBack();
            $message = 'Error deleting job: ' . $e->getMessage();
        }
    } else {
        header('Location: ' . route_url('admin_jobs'));
        exit;
    }
}

$page_title = 'Borrar Oferta de Empleo';

$content = '
<div class="row">
    <div class="col-md-6">
        <div class="card card-danger">
            <div class="card-header text-black">
                <h3 class="card-title">Eliminar Oferta de Trabajo</h3>
            </div>
            <div class="card-body">
                <p>¿Estás seguro de que deseas eliminar esta oferta de trabajo?</p>
                <div class="alert alert-warning">
                    <strong>Advertencia:</strong> Esta acción no se puede deshacer. También eliminará:
                    <ul>
                        <li>Todas las postulaciones para esta oferta (' . $app_count . ' postulaciones)</li>
                    </ul>
                </div>
                <div class="job-info">
                    <h5>Detalles de la Oferta:</h5>
                    <p><strong>Título:</strong> ' . htmlspecialchars($job['title']) . '</p>
                    <p><strong>Empresa:</strong> ' . htmlspecialchars($job['company_name']) . '</p>
                    <p><strong>Postulaciones:</strong> ' . $app_count . '</p>
                    <p><strong>Publicado:</strong> ' . date('d/m/Y', strtotime($job['posted_date'])) . '</p>
                </div>
                ' . ($message ? '<div class="alert alert-danger">' . $message . '</div>' : '') . '
            </div>
            <div class="card-footer">
                <form method="post" style="display: inline;">
                    <input type="hidden" name="confirm_delete" value="1">
                    <button type="submit" class="btn btn-danger" onclick="return confirm(\'¿Estás completamente seguro? Esto eliminará todas las postulaciones asociadas.\')">Eliminar Oferta</button>
                </form>
                <a href="' . route_url('admin_jobs') . '" class="btn btn-secondary">Cancelar</a>
            </div>
        </div>
    </div>
</div>';

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