<?php
session_start();
require_once "config/database.php";

if (!isset($_SESSION['user_id']) || ($_SESSION['role'] ?? '') === 'guest') {
    header("Location: login.php");
    exit;
}

$database = new Database();
$db = $database->getConnection();

if (!$db) {
    die("Database connection failed.");
}

$user_id = (int) ($_SESSION['user_id'] ?? 0);

/*
|--------------------------------------------------------------------------
| Workspace logic
|--------------------------------------------------------------------------
*/
$workspace_type = $_SESSION['workspace_type'] ?? 'personal';
$active_company_id = isset($_SESSION['active_company_id']) && $_SESSION['active_company_id'] !== ''
    ? (int) $_SESSION['active_company_id']
    : null;

$is_company_workspace = ($workspace_type === 'company' && $active_company_id !== null);

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

if ($id <= 0) {
    header("Location: contracts.php");
    exit;
}

function bindOwnership(PDOStatement $stmt, bool $is_company_workspace, ?int $active_company_id, int $user_id): void
{
    if ($is_company_workspace) {
        $stmt->bindValue(":company_id", $active_company_id, PDO::PARAM_INT);
    } else {
        $stmt->bindValue(":user_id", $user_id, PDO::PARAM_INT);
    }
}

/*
|--------------------------------------------------------------------------
| Ownership logic
|--------------------------------------------------------------------------
*/
if ($is_company_workspace) {
    $ownershipSql = "id = :id AND company_id = :company_id";
} else {
    $ownershipSql = "id = :id AND user_id = :user_id AND company_id IS NULL";
}

/*
|--------------------------------------------------------------------------
| Load contract with ownership check
|--------------------------------------------------------------------------
*/
$stmt = $db->prepare("
    SELECT id, file_path
    FROM contracts
    WHERE {$ownershipSql}
    LIMIT 1
");
$stmt->bindValue(":id", $id, PDO::PARAM_INT);
bindOwnership($stmt, $is_company_workspace, $active_company_id, $user_id);
$stmt->execute();
$contract = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$contract) {
    header("Location: contracts.php");
    exit;
}

/*
|--------------------------------------------------------------------------
| Delete contract row
|--------------------------------------------------------------------------
*/
$deleteStmt = $db->prepare("
    DELETE FROM contracts
    WHERE {$ownershipSql}
");
$deleteStmt->bindValue(":id", $id, PDO::PARAM_INT);
bindOwnership($deleteStmt, $is_company_workspace, $active_company_id, $user_id);
$deleteStmt->execute();

/*
|--------------------------------------------------------------------------
| Delete physical file only if DB delete succeeded
|--------------------------------------------------------------------------
*/
if ($deleteStmt->rowCount() > 0 && !empty($contract['file_path'])) {
    $fullPath = __DIR__ . '/' . ltrim($contract['file_path'], '/');

    if (file_exists($fullPath) && is_file($fullPath)) {
        unlink($fullPath);
    }
}

header("Location: contracts.php");
exit;
?>