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

check_role('empresa');

$user = get_logged_in_user();

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

if (!$company) {
    die('Company profile not found.');
}

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

if (!$job_id) {
    header('Location: /company/jobs');
    exit;
}

// Get job details and verify ownership
$stmt = $pdo->prepare("SELECT * FROM jobs WHERE id = ? AND company_id = ?");
$stmt->execute([$job_id, $company['id']]);
$job = $stmt->fetch();

if (!$job) {
    header('Location: /company/jobs');
    exit;
}

$message = '';
$confirm = isset($_GET['confirm']) ? $_GET['confirm'] : '';

if ($confirm == 'yes') {
    try {
        // Start transaction
        $pdo->beginTransaction();

        // Delete applications first
        $stmt = $pdo->prepare("DELETE FROM applications WHERE job_id = ?");
        $stmt->execute([$job_id]);

        // Delete job
        $stmt = $pdo->prepare("DELETE FROM jobs WHERE id = ? AND company_id = ?");
        $stmt->execute([$job_id, $company['id']]);

        $pdo->commit();
        header('Location: /company/jobs?success=job_deleted');
        exit;

    } catch (Exception $e) {
        $pdo->rollBack();
        $message = 'Error deleting job: ' . $e->getMessage();
    }
}

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

$page_title = 'Delete Job - ' . htmlspecialchars($job['title']);

$content = '
<div class="row">
    <div class="col-md-6">
        <div class="card card-danger">
            <div class="card-header">
                <h3 class="card-title">Delete Job</h3>
            </div>
            <div class="card-body">
                ' . ($message ? '<div class="alert alert-danger">' . $message . '</div>' : '') . '
                <div class="alert alert-warning">
                    <h5><i class="icon fas fa-exclamation-triangle"></i> Warning!</h5>
                    You are about to delete the job <strong>"' . htmlspecialchars($job['title']) . '"</strong>.
                    <br><br>
                    This action cannot be undone and will also delete:
                    <ul>
                        <li>All applications to this job (' . $app_count . ' application' . ($app_count != 1 ? 's' : '') . ')</li>
                        <li>All application data and communications</li>
                    </ul>
                </div>
                <div class="text-center">
                    <h4>' . htmlspecialchars($job['title']) . '</h4>
                    <p class="text-muted">' . htmlspecialchars($company['company_name']) . '</p>
                    <div class="row text-center">
                        <div class="col-6">
                            <div class="small-box bg-info">
                                <div class="inner">
                                    <h3>' . $app_count . '</h3>
                                    <p>Applications</p>
                                </div>
                            </div>
                        </div>
                        <div class="col-6">
                            <div class="small-box bg-' . ($job['status'] == 'active' ? 'success' : 'secondary') . '">
                                <div class="inner">
                                    <h3>' . ucfirst($job['status']) . '</h3>
                                    <p>Status</p>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="card-footer">
                <a href="/company/jobs/' . $job_id . '/delete?confirm=yes" class="btn btn-danger" onclick="return confirm(\'Are you absolutely sure you want to delete this job? This action cannot be undone and will delete all applications.\')">
                    <i class="fas fa-trash"></i> Yes, Delete Job
                </a>
                <a href="/company/jobs/' . $job_id . '" class="btn btn-secondary float-right">
                    <i class="fas fa-times"></i> Cancel
                </a>
            </div>
        </div>
    </div>
    <div class="col-md-6">
        <div class="card">
            <div class="card-header">
                <h3 class="card-title">Job Information</h3>
            </div>
            <div class="card-body">
                <dl class="row">
                    <dt class="col-sm-4">Job Title:</dt>
                    <dd class="col-sm-8">' . htmlspecialchars($job['title']) . '</dd>

                    <dt class="col-sm-4">Company:</dt>
                    <dd class="col-sm-8">' . htmlspecialchars($company['company_name']) . '</dd>

                    <dt class="col-sm-4">Location:</dt>
                    <dd class="col-sm-8">' . htmlspecialchars($job['location'] ?: 'Not specified') . '</dd>

                    <dt class="col-sm-4">Salary:</dt>
                    <dd class="col-sm-8">' . ($job['salary'] ? '$' . number_format($job['salary'], 2) : 'Not specified') . '</dd>

                    <dt class="col-sm-4">Job Type:</dt>
                    <dd class="col-sm-8">' . ucfirst($job['job_type']) . '</dd>

                    <dt class="col-sm-4">Status:</dt>
                    <dd class="col-sm-8">' . ($job['status'] == 'active' ? '<span class="badge badge-success">Active</span>' : '<span class="badge badge-secondary">' . ucfirst($job['status']) . '</span>') . '</dd>

                    <dt class="col-sm-4">Posted:</dt>
                    <dd class="col-sm-8">' . date('F d, Y', strtotime($job['posted_date'])) . '</dd>

                    <dt class="col-sm-4">Applications:</dt>
                    <dd class="col-sm-8">' . $app_count . '</dd>
                </dl>
            </div>
        </div>
    </div>
</div>';

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