<?php
declare(strict_types=1);

require_once __DIR__ . '/_bootstrap.php';
require_once dirname(__DIR__) . '/common/customer_cleanup.php';

$master = golder_master_pdo();
$jobId = golder_job_start($master, 'cron_demo_hard_delete');

try {
    $candidateIds = golder_demo_hard_delete_candidates($master, 48, 500);
    $deleted = 0;
    $failed = 0;
    $errors = [];

    foreach ($candidateIds as $customerId) {
        $res = golder_hard_delete_customer($master, (int)$customerId);
        if (!empty($res['ok'])) {
            $deleted++;
            continue;
        }
        $failed++;
        $errors[] = [
            'customer_id' => (int)$customerId,
            'message' => (string)($res['message'] ?? 'unknown'),
        ];
    }

    golder_job_finish($master, $jobId, 'ok', [
        'candidate_count' => count($candidateIds),
        'deleted' => $deleted,
        'failed' => $failed,
        'errors' => array_slice($errors, 0, 30),
    ]);
    echo "cron_demo_hard_delete finished. deleted={$deleted} failed={$failed}\n";
} catch (Throwable $e) {
    golder_job_finish($master, $jobId, 'error', ['error' => $e->getMessage()]);
    echo "cron_demo_hard_delete failed: {$e->getMessage()}\n";
    exit(1);
}


