import fs from 'node:fs';
import type { Logger } from './logger.js';

let lastDeployLockSeenAt: number | null = null;

export function shouldSkipChecks(
  lockPath: string,
  gracePeriodMs: number,
  logger: Logger,
): boolean {
  const lockExists = fs.existsSync(lockPath);

  if (lockExists) {
    lastDeployLockSeenAt = Date.now();
    logger.info('Deploy lock detected — skipping checks');
    return true;
  }

  if (lastDeployLockSeenAt !== null) {
    const elapsed = Date.now() - lastDeployLockSeenAt;
    if (elapsed < gracePeriodMs) {
      const remaining = Math.ceil((gracePeriodMs - elapsed) / 1000);
      logger.info(`Deploy grace period — ${remaining}s remaining, skipping checks`);
      return true;
    }
    logger.info('Deploy grace period ended — resuming checks');
    lastDeployLockSeenAt = null;
  }

  return false;
}
