<?php
/**
 * Super Admin - Deletar Loja
 */
require_once __DIR__ . '/../../../includes/config.php';

if (!isSuperAdmin()) {
    header('Location: /pages/login.php');
    exit;
}

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

if ($storeId <= 0) {
    header('Location: /pages/superadmin/stores/index.php?error=invalid_id');
    exit;
}

$pdo = getDB();

try {
    // Verificar se a loja existe
    $stmt = $pdo->prepare("SELECT id, name FROM stores WHERE id = ? AND deleted_at IS NULL");
    $stmt->execute([$storeId]);
    $store = $stmt->fetch(PDO::FETCH_ASSOC);
    
    if (!$store) {
        header('Location: /pages/superadmin/stores/index.php?error=store_not_found');
        exit;
    }
    
    // Soft delete - apenas marca como deletada
    $stmt = $pdo->prepare("UPDATE stores SET deleted_at = NOW() WHERE id = ?");
    $stmt->execute([$storeId]);
    
    header('Location: /pages/superadmin/stores/index.php?success=store_deleted');
    exit;
    
} catch (PDOException $e) {
    error_log("Erro ao deletar loja: " . $e->getMessage());
    header('Location: /pages/superadmin/stores/index.php?error=delete_failed');
    exit;
}
