/**
 * SDK flow definition — phase/step types + ordered step list + navigation helpers.
 *
 * Phase 4: extracted from state.ts so the root state module stays under the
 * file-size cap.
 */

// =============================================================================
// SDK FLOW & NAVIGATION
// =============================================================================

export type SDKPhase = 1 | 2

export interface SDKStep {
  id: string
  phase: SDKPhase
  order: number
  name: string
  nameShort: string
  description: string
  url: string
  checkpointId: string
  prerequisiteSteps: string[]
  isOptional: boolean
}

export const SDK_STEPS: SDKStep[] = [
  // Phase 1: Automatisches Compliance Assessment
  {
    id: 'use-case-workshop',
    phase: 1,
    order: 1,
    name: 'Use Case Workshop',
    nameShort: 'Use Case',
    description: '5-Schritte-Wizard für Use Case Erfassung',
    url: '/sdk/advisory-board',
    checkpointId: 'CP-UC',
    prerequisiteSteps: [],
    isOptional: false,
  },
  {
    id: 'screening',
    phase: 1,
    order: 2,
    name: 'System Screening',
    nameShort: 'Screening',
    description: 'SBOM + Security Check',
    url: '/sdk/screening',
    checkpointId: 'CP-SCAN',
    prerequisiteSteps: ['use-case-workshop'],
    isOptional: false,
  },
  {
    id: 'modules',
    phase: 1,
    order: 3,
    name: 'Compliance Modules',
    nameShort: 'Module',
    description: 'Abgleich welche Regulierungen gelten',
    url: '/sdk/modules',
    checkpointId: 'CP-MOD',
    prerequisiteSteps: ['screening'],
    isOptional: false,
  },
  {
    id: 'requirements',
    phase: 1,
    order: 4,
    name: 'Requirements',
    nameShort: 'Anforderungen',
    description: 'Prüfaspekte aus Regulierungen ableiten',
    url: '/sdk/requirements',
    checkpointId: 'CP-REQ',
    prerequisiteSteps: ['modules'],
    isOptional: false,
  },
  {
    id: 'controls',
    phase: 1,
    order: 5,
    name: 'Controls',
    nameShort: 'Controls',
    description: 'Erforderliche Maßnahmen ermitteln',
    url: '/sdk/controls',
    checkpointId: 'CP-CTRL',
    prerequisiteSteps: ['requirements'],
    isOptional: false,
  },
  {
    id: 'evidence',
    phase: 1,
    order: 6,
    name: 'Evidence',
    nameShort: 'Nachweise',
    description: 'Nachweise dokumentieren',
    url: '/sdk/evidence',
    checkpointId: 'CP-EVI',
    prerequisiteSteps: ['controls'],
    isOptional: false,
  },
  {
    id: 'audit-checklist',
    phase: 1,
    order: 7,
    name: 'Audit Checklist',
    nameShort: 'Checklist',
    description: 'Prüfliste generieren',
    url: '/sdk/audit-checklist',
    checkpointId: 'CP-CHK',
    prerequisiteSteps: ['evidence'],
    isOptional: false,
  },
  {
    id: 'risks',
    phase: 1,
    order: 8,
    name: 'Risk Matrix',
    nameShort: 'Risiken',
    description: 'Risikobewertung & Residual Risk',
    url: '/sdk/risks',
    checkpointId: 'CP-RISK',
    prerequisiteSteps: ['audit-checklist'],
    isOptional: false,
  },
  // Phase 2: Dokumentengenerierung
  {
    id: 'ai-act',
    phase: 2,
    order: 1,
    name: 'AI Act Klassifizierung',
    nameShort: 'AI Act',
    description: 'Risikostufe nach EU AI Act',
    url: '/sdk/ai-act',
    checkpointId: 'CP-AI',
    prerequisiteSteps: ['risks'],
    isOptional: false,
  },
  {
    id: 'obligations',
    phase: 2,
    order: 2,
    name: 'Pflichtenübersicht',
    nameShort: 'Pflichten',
    description: 'NIS2, DSGVO, AI Act Pflichten',
    url: '/sdk/obligations',
    checkpointId: 'CP-OBL',
    prerequisiteSteps: ['ai-act'],
    isOptional: false,
  },
  {
    id: 'dsfa',
    phase: 2,
    order: 3,
    name: 'DSFA',
    nameShort: 'DSFA',
    description: 'Datenschutz-Folgenabschätzung',
    url: '/sdk/dsfa',
    checkpointId: 'CP-DSFA',
    prerequisiteSteps: ['obligations'],
    isOptional: true,
  },
  {
    id: 'tom',
    phase: 2,
    order: 4,
    name: 'TOMs',
    nameShort: 'TOMs',
    description: 'Technische & Org. Maßnahmen',
    url: '/sdk/tom',
    checkpointId: 'CP-TOM',
    prerequisiteSteps: ['dsfa'],
    isOptional: false,
  },
  {
    id: 'loeschfristen',
    phase: 2,
    order: 5,
    name: 'Löschfristen',
    nameShort: 'Löschfristen',
    description: 'Aufbewahrungsrichtlinien',
    url: '/sdk/loeschfristen',
    checkpointId: 'CP-RET',
    prerequisiteSteps: ['tom'],
    isOptional: false,
  },
  {
    id: 'vvt',
    phase: 2,
    order: 6,
    name: 'Verarbeitungsverzeichnis',
    nameShort: 'VVT',
    description: 'Art. 30 DSGVO Dokumentation',
    url: '/sdk/vvt',
    checkpointId: 'CP-VVT',
    prerequisiteSteps: ['loeschfristen'],
    isOptional: false,
  },
  {
    id: 'consent',
    phase: 2,
    order: 7,
    name: 'Rechtliche Vorlagen',
    nameShort: 'Vorlagen',
    description: 'AGB, Datenschutz, Nutzungsbedingungen',
    url: '/sdk/consent',
    checkpointId: 'CP-DOC',
    prerequisiteSteps: ['vvt'],
    isOptional: false,
  },
  {
    id: 'cookie-banner',
    phase: 2,
    order: 8,
    name: 'Cookie Banner',
    nameShort: 'Cookies',
    description: 'Cookie-Consent Generator',
    url: '/sdk/cookie-banner',
    checkpointId: 'CP-COOK',
    prerequisiteSteps: ['consent'],
    isOptional: false,
  },
  {
    id: 'einwilligungen',
    phase: 2,
    order: 9,
    name: 'Einwilligungen',
    nameShort: 'Consents',
    description: 'Consent-Tracking Setup',
    url: '/sdk/einwilligungen',
    checkpointId: 'CP-CONS',
    prerequisiteSteps: ['cookie-banner'],
    isOptional: false,
  },
  {
    id: 'dsr',
    phase: 2,
    order: 10,
    name: 'DSR Portal',
    nameShort: 'DSR',
    description: 'Betroffenenrechte-Portal',
    url: '/sdk/dsr',
    checkpointId: 'CP-DSR',
    prerequisiteSteps: ['einwilligungen'],
    isOptional: false,
  },
  {
    id: 'escalations',
    phase: 2,
    order: 11,
    name: 'Escalations',
    nameShort: 'Eskalationen',
    description: 'Management-Workflows',
    url: '/sdk/escalations',
    checkpointId: 'CP-ESC',
    prerequisiteSteps: ['dsr'],
    isOptional: false,
  },
]

// =============================================================================
// HELPER FUNCTIONS
// =============================================================================

export function getStepById(stepId: string): SDKStep | undefined {
  return SDK_STEPS.find(s => s.id === stepId)
}

export function getStepByUrl(url: string): SDKStep | undefined {
  return SDK_STEPS.find(s => s.url === url)
}

export function getStepsForPhase(phase: SDKPhase): SDKStep[] {
  return SDK_STEPS.filter(s => s.phase === phase).sort((a, b) => a.order - b.order)
}

export function getNextStep(currentStepId: string): SDKStep | undefined {
  const currentStep = getStepById(currentStepId)
  if (!currentStep) return undefined

  const stepsInPhase = getStepsForPhase(currentStep.phase)
  const currentIndex = stepsInPhase.findIndex(s => s.id === currentStepId)

  if (currentIndex < stepsInPhase.length - 1) {
    return stepsInPhase[currentIndex + 1]
  }

  if (currentStep.phase === 1) {
    return getStepsForPhase(2)[0]
  }

  return undefined
}

export function getPreviousStep(currentStepId: string): SDKStep | undefined {
  const currentStep = getStepById(currentStepId)
  if (!currentStep) return undefined

  const stepsInPhase = getStepsForPhase(currentStep.phase)
  const currentIndex = stepsInPhase.findIndex(s => s.id === currentStepId)

  if (currentIndex > 0) {
    return stepsInPhase[currentIndex - 1]
  }

  if (currentStep.phase === 2) {
    const phase1Steps = getStepsForPhase(1)
    return phase1Steps[phase1Steps.length - 1]
  }

  return undefined
}
