<?php
declare(strict_types=1);
header('Content-Type: application/json; charset=utf-8');

$METHODS_DEFAULT = ['saisonalitaet100'];
$table = 'saisonaleTable';

require_once __DIR__ . '/../../engine/engine.php';
$pdo = null;
if (isset($GLOBALS['pdo']) && $GLOBALS['pdo'] instanceof PDO) {
	$pdo = $GLOBALS['pdo'];
} elseif (function_exists('db')) {
	$pdo = db();
}
if (!$pdo instanceof PDO) {
	http_response_code(500);
	echo json_encode(['success' => false, 'message' => 'DB connection not available']);
	exit;
}

$methodStr = trim((string)($_POST['method'] ?? ''));
$methods = $methodStr !== '' ? array_filter(array_map('trim', explode(',', $methodStr))) : $METHODS_DEFAULT;
$scanId = trim((string)($_POST['scan_id'] ?? ''));

$params = $methods;
$where = 'WHERE method IN (' . implode(',', array_fill(0, count($methods), '?')) . ')';
if ($scanId !== '') {
	$where .= ' AND scan_id = ?';
	$params[] = $scanId;
}

$sql = "DELETE FROM {$table} {$where}";
$stmt = $pdo->prepare($sql);
$stmt->execute($params);
$deleted = $stmt->rowCount();

echo json_encode(['success' => true, 'deleted_rows' => $deleted]);