<?php
if (session_status() === PHP_SESSION_NONE) { session_start(); }
require_once __DIR__ . '/../config/database.php';
require_once __DIR__ . '/../config/helpers.php';

// Permissions Check
if (!can('bank_accounts','delete')) {
    die('Access Denied');
}

$id = $_GET['id'] ?? 0;

if ($id > 0) {
    try {
        // التحقق مما إذا كان الحساب مرتبطاً بعمليات مالية قبل الحذف (اختياري لكنه احترافي)
        // $stmt = $pdo->prepare("SELECT COUNT(*) FROM transactions WHERE bank_id = ?");
        // $stmt->execute([$id]);
        // if ($stmt->fetchColumn() > 0) { die("Cannot delete: Account has linked transactions."); }

        $stmt = $pdo->prepare("DELETE FROM bank_accounts WHERE bank_id = ?");
        $stmt->execute([$id]);
        
        header("Location: bank_accounts.php?deleted=1");
        exit;
    } catch (PDOException $e) {
        die("Error deleting record: " . $e->getMessage());
    }
} else {
    header("Location: bank_accounts.php");
    exit;
}