// frontend/src/lib/models/Migration.WizardModel.svelte.ts
// #region Migration.WizardModel [C:3] [TYPE Model] [SEMANTICS migration,wizard,step-navigation]
// @ingroup Migration
// @BRIEF Wizard step navigation sub-model — currentStep, gated goToStep(), clearError().
// @RELATION DEPENDS_ON -> [Migration.Model]
// @INVARIANT Backward navigation (step <= currentStep) is always allowed.
// @INVARIANT Step 2 requires source !== target and both envs selected (delegates to parent.stepReady).
// @INVARIANT Step 3 requires step 1 and step 2 ready.
// @INVARIANT Step 4 requires a dry-run result.
// @STATE currentStep — Active wizard step (1=environments, 2=dashboards, 3=review, 4=execute).
// @ACTION goToStep(step) — Navigates to a wizard step with readiness gating.
// @ACTION clearError() — Clears the error message on the parent model.

import type { MigrationModel } from "./MigrationModel.svelte.ts";

export class WizardState {
  parent: MigrationModel;
  currentStep: number = $state(1);

  constructor(parent: MigrationModel) {
    this.parent = parent;
  }

  /** Navigate to a wizard step. Gated by step readiness and dry-run result. */
  goToStep(step: number): void {
    const dryRunResult = this.parent.executor?.dryRunResult ?? null;
    if (
      step <= this.currentStep ||
      (step === 2 && this.parent.stepReady[1]) ||
      (step === 3 && this.parent.stepReady[1] && this.parent.stepReady[2]) ||
      (step === 4 && dryRunResult)
    ) {
      this.currentStep = step;
    }
  }

  /** Clear error message on the parent model. */
  clearError(): void {
    this.parent.error = "";
  }
}
// #endregion Migration.WizardModel
