/**
 * auditor-partition-maintenance smoke — guards the 2026-06-01 T2E fix.
 *
 * Auditor finding: the Heartbeat RANGE partitioning pre-created
 * monthly partitions through Aug 2026 only, with a pmax catch-all
 * after. Maintenance was a manual runbook. Without automation the
 * pmax fills indefinitely once the clock crosses Aug 2026, costing
 * the perf benefit of partitioned DROPs for old data.
 *
 * Fix: PartitionMaintenanceService + a daily worker tick that
 * REORGANIZE pmax INTO future-month partitions and DROP partitions
 * older than the retention window. This smoke asserts the wiring.
 */
import './_lib';
import { ok, fail } from './_lib';
import { readFileSync, existsSync } from 'fs';
import path from 'path';

const ROOT = path.resolve(__dirname, '../..');
const read = (rel: string): string => readFileSync(path.join(ROOT, rel), 'utf8');

function assert(label: string, cond: boolean, detail: string): void {
    if (!cond) fail(label, detail);
    ok(label);
}

function main(): void {
    // Service file exists.
    const svcPath = 'src/lib/services/partition-maintenance.service.ts';
    assert('service file present', existsSync(path.join(ROOT, svcPath)), `${svcPath} missing`);
    const svc = read(svcPath);

    assert('service exports planPartitionMaintenance (pure planner)',
        /export function planPartitionMaintenance/.test(svc),
        'planPartitionMaintenance export missing.');
    assert('service exports PartitionMaintenanceService',
        /export class PartitionMaintenanceService/.test(svc),
        'PartitionMaintenanceService class missing.');

    // SQL ddl uses REORGANIZE pmax and DROP PARTITION.
    assert('service issues REORGANIZE PARTITION pmax INTO ...',
        /REORGANIZE PARTITION pmax INTO/.test(svc),
        'service no longer issues REORGANIZE pmax — adds will fail.');
    assert('service issues DROP PARTITION',
        /DROP PARTITION/.test(svc),
        'service no longer issues DROP PARTITION — retention cleanup will fail.');

    // Worker wiring.
    const worker = read('scripts/worker.ts');
    assert('worker imports PartitionMaintenanceService',
        /PartitionMaintenanceService/.test(worker),
        'worker.ts no longer imports PartitionMaintenanceService — daily tick is dead.');
    assert('worker registers a daily partition tick',
        /PARTITION_MAINTENANCE_TICK_MS/.test(worker) && /24\s*\*\s*60\s*\*\s*60_000/.test(worker),
        'worker.ts no longer sets a 24h partition-maintenance tick.');
    assert('worker fires partition tick shortly after boot',
        /setTimeout\(\(\)\s*=>\s*void runPartitionTick/.test(worker),
        'worker.ts no longer fires the partition tick at startup.');
    assert('worker drain waits for in-flight partition tick',
        /isRunningPartitionTick/.test(worker),
        'worker shutdown drain no longer waits for the partition tick.');

    // Env vars documented.
    const env = read('.env.example');
    assert('.env.example documents PARTITION_LOOKAHEAD_MONTHS',
        /PARTITION_LOOKAHEAD_MONTHS=/.test(env),
        'PARTITION_LOOKAHEAD_MONTHS missing from .env.example.');
    assert('.env.example documents PARTITION_RETENTION_MONTHS',
        /PARTITION_RETENTION_MONTHS=/.test(env),
        'PARTITION_RETENTION_MONTHS missing from .env.example.');

    // Tests exist.
    assert('planner has unit tests',
        existsSync(path.join(ROOT, 'src/lib/services/__tests__/partition-maintenance.service.test.ts')),
        'partition-maintenance.service.test.ts missing.');

    ok('auditor-partition-maintenance', 'partition maintenance is automated daily');
}

main();
