<?php
session_start();
require_once __DIR__ . '/../../config/db.php';

if (!isset($_SESSION['user_id'])) {
    header("Location: ../../auth/login.php");
    exit;
}

$user_id = $_SESSION['user_id'];
$role = $_SESSION['role'];

// Only admins & support can close
if (!in_array($role, ['admin', 'support'])) {
    header("Location: ../issues/?error=unauthorized");
    exit;
}

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['ticket_id'])) {
    $ticket_id = (int) $_POST['ticket_id'];

    $stmt = $pdo->prepare("UPDATE tickets SET status = 'closed', updated_at = NOW() WHERE id = ?");
    $stmt->execute([$ticket_id]);

    header("Location: ../issues/?success=closed");
    exit;
} else {
    header("Location: ../issues/?error=invalid_request");
    exit;
}
