import type { ISignalPipelineProvider } from './ISignalPipelineProvider'
import type { PipelineState } from '../domain/signalPipeline'
import { GateStatus, PipelineStage } from '../domain/signalPipeline'
import type { GateType } from '../domain/signalPipeline'
import { MOCK_PIPELINE_STATES, MOCK_AUDIT_TRAILS } from '../mock-data/signalPipelines'

let states: PipelineState[] = [...MOCK_PIPELINE_STATES]

export const MockupSignalPipelineProvider: ISignalPipelineProvider = {
  async getPipelineState(signalId) {
    return states.find(s => s.signalId === signalId) ?? null
  },
  async evaluateGate(signalId, gateType) {
    const idx = states.findIndex(s => s.signalId === signalId)
    if (idx === -1) throw new Error(`Pipeline state for ${signalId} not found`)
    // Demo: mark gate as re-evaluated (no real logic change)
    const updated: PipelineState = {
      ...states[idx],
      gates: {
        ...states[idx].gates,
        [gateType]: {
          ...states[idx].gates[gateType as GateType],
          evaluatedAt: new Date().toISOString(),
        },
      },
    }
    states[idx] = updated
    return updated
  },
  async getAuditTrail(signalId) {
    return MOCK_AUDIT_TRAILS.filter(e => e.signalId === signalId)
      .sort((a, b) => b.timestamp.localeCompare(a.timestamp))
  },
  async publishToFutureAvailability(signalId) {
    const idx = states.findIndex(s => s.signalId === signalId)
    if (idx === -1) throw new Error(`Pipeline state for ${signalId} not found`)
    const now = new Date().toISOString()
    states[idx] = {
      ...states[idx],
      publishedToFutureAvailability: true,
      publishedAt: now,
      currentStage: PipelineStage.STAGE_6_MATCHABLE_RESULT,
      gates: {
        ...states[idx].gates,
        FEED_ELIGIBILITY_GATE: {
          ...states[idx].gates.FEED_ELIGIBILITY_GATE,
          status: GateStatus.PASSED,
          reason: 'Signal manuell in Future Availability publiziert.',
          evaluatedAt: now,
          checks: [{ label: 'Manuell publiziert', passed: true, value: 'Ja' }],
        },
      },
      overallEligible: true,
    }
    return states[idx]
  },
}
