/**
 * M-AI-03-03: AiJob 状态机错误类型
 *
 * 所有违反状态机的操作抛出这些类型化错误，
 * 调用方可区分"Job 已被抢走"、"Job 已结束"等场景。
 */

export class JobLockConflictError extends Error {
  public readonly code = 'JOB_LOCK_CONFLICT';
  constructor(public readonly jobId: string) {
    super(`Job ${jobId} is not in 'queued' status or already locked by another worker`);
    this.name = 'JobLockConflictError';
  }
}

export class JobNotActiveError extends Error {
  public readonly code = 'JOB_NOT_ACTIVE';
  constructor(public readonly jobId: string, public readonly currentStatus: string) {
    super(`Job ${jobId} is in '${currentStatus}' status, expected 'running' or 'locked'`);
    this.name = 'JobNotActiveError';
  }
}

export class JobAlreadyTerminalError extends Error {
  public readonly code = 'JOB_ALREADY_TERMINAL';
  constructor(public readonly jobId: string, public readonly currentStatus: string) {
    super(`Job ${jobId} is already in terminal status '${currentStatus}'`);
    this.name = 'JobAlreadyTerminalError';
  }
}

export class JobNotCancellableError extends Error {
  public readonly code = 'JOB_NOT_CANCELLABLE';
  constructor(public readonly jobId: string, public readonly currentStatus: string) {
    super(`Job ${jobId} in '${currentStatus}' status cannot be cancelled`);
    this.name = 'JobNotCancellableError';
  }
}

export class JobExtendLockError extends Error {
  public readonly code = 'JOB_EXTEND_LOCK_ERROR';
  constructor(public readonly jobId: string, public readonly instanceId: string) {
    super(`Cannot extend lock for job ${jobId}: not locked by instance '${instanceId}'`);
    this.name = 'JobExtendLockError';
  }
}

export class IllegalStateTransitionError extends Error {
  public readonly code = 'ILLEGAL_STATE_TRANSITION';
  constructor(
    public readonly jobId: string,
    public readonly from: string,
    public readonly to: string,
  ) {
    super(`Illegal state transition for job ${jobId}: '${from}' → '${to}'`);
    this.name = 'IllegalStateTransitionError';
  }
}
