<?php
// POST { import_id } -> zeros mailbox temp_password_encrypted; stamps cleared_by/at.

require_once('../auto-prepend.php');
require_once('../libs/CpanelImportSummary.php');
require_once('../libs/mysqlmgmt.php');

if (!defined('AUTH_USER')) {
    http_response_code(401); header('Content-Type: application/json');
    echo json_encode(['error' => 'Not authenticated']); exit;
}
header('Content-Type: application/json');
if (($_SERVER['REQUEST_METHOD'] ?? '') !== 'POST') {
    http_response_code(405); echo json_encode(['error' => 'POST required']); exit;
}

$importId = $_POST['import_id'] ?? '';
if (!preg_match('/^import_[A-Za-z0-9]+$/', $importId)) {
    http_response_code(400); echo json_encode(['error' => 'invalid import_id']); exit;
}

$pdo = (new mysqlmgmt())->getMySQLConnection();
$pdo->exec('USE whp');
$stmt = $pdo->prepare('SELECT username FROM cpanel_imports WHERE import_id = ?');
$stmt->execute([$importId]);
$owner = $stmt->fetchColumn();
if (!$owner || ($owner !== AUTH_USER && AUTH_USER !== 'root')) {
    http_response_code(403); echo json_encode(['error' => 'forbidden']); exit;
}

$ok = CpanelImportSummary::clearCredentials($importId, AUTH_USER);
echo json_encode(['success' => $ok]);
