import type { AmplificationGate, AmplificationSparkProps } from '../AmplificationSpark'
import type { PrQueueItem, PrQueueRepoCompleteness } from '../PrQueueTable'
import type { RepoCiSummary } from '../RepoCiTile'
import type { RunnerFleetRunner } from '../RunnerFleetStrip'
import type { TrainViewModel } from '../TrainCard'

export const CI_REPO_SUMMARY: RepoCiSummary = {
  repo: 'alexcodeplace/multideal',
  queueComplete: true,
  historyComplete: true,
  trainRunsComplete: true,
  runnersComplete: true,
  refsComplete: true,
  prsComplete: true,
  trainsComplete: true,
  prQueueDepth: 7,
  queueDepth: 3,
  runnersBusy: 3,
  runnersTotal: 4,
  landsToday: 5,
  oldestQueuedAgeH: 0.75,
  degraded: [],
}

export const CI_REPO_SECONDARY_SUMMARY: RepoCiSummary = {
  ...CI_REPO_SUMMARY,
  repo: 'alexcodeplace/overdeck',
  prQueueDepth: 2,
  queueDepth: 1,
  runnersBusy: 1,
  oldestQueuedAgeH: 0.5,
}

export const CI_REPO_SUMMARIES: RepoCiSummary[] = [
  CI_REPO_SUMMARY,
  CI_REPO_SECONDARY_SUMMARY,
]

export const CI_REPO_GAP_SUMMARY: RepoCiSummary = {
  repo: 'alexcodeplace/platform',
  queueComplete: false,
  historyComplete: false,
  trainRunsComplete: false,
  runnersComplete: false,
  refsComplete: false,
  prsComplete: false,
  trainsComplete: false,
  prQueueDepth: null,
  queueDepth: null,
  runnersBusy: null,
  runnersTotal: null,
  landsToday: null,
  oldestQueuedAgeH: null,
  degraded: ['budget', 'pagination', 'jobs'],
  pollDeferred: false,
}

export const CI_REPO_DEFERRED_SUMMARY: RepoCiSummary = {
  ...CI_REPO_SUMMARY,
  repo: 'alexcodeplace/mega-plan-harness',
  pollDeferred: true,
  degraded: [],
}

export const CI_TRAIN: TrainViewModel = {
  repo: 'alexcodeplace/multideal',
  prNumber: 901,
  prUrl: 'https://github.com/alexcodeplace/multideal/pull/901',
  branch: 'integration/batch-train-12',
  createdAt: '2026-07-19T08:00:00.000Z',
  members: [79, 74],
  state: 'failed',
  stateGap: null,
  gateRun: {
    id: 123456,
    attempt: 1,
    status: 'completed',
    conclusion: 'failure',
    jobsComplete: true,
    jobs: [
      {
        id: 41,
        name: 'Integration database lanes',
        status: 'completed',
        conclusion: 'failure',
        runnerId: 3,
        runner: 'debian-1',
        startedAt: '2026-07-19T08:00:00.000Z',
        completedAt: '2026-07-19T08:05:00.000Z',
        infraFlag: true,
      },
      {
        id: 42,
        name: 'Unit tests',
        status: 'completed',
        conclusion: 'success',
        runnerId: 4,
        runner: 'debian-2',
        startedAt: '2026-07-19T08:00:00.000Z',
        completedAt: '2026-07-19T08:03:00.000Z',
        infraFlag: false,
      },
    ],
  },
  blockedBy: 'Integration database lanes — ENOTEMPTY (infra)',
  infraFlag: true,
  refsComplete: true,
  prsComplete: true,
  trainRunsComplete: true,
  trainsComplete: true,
}

export const CI_TRAIN_SECONDARY: TrainViewModel = {
  ...CI_TRAIN,
  repo: 'alexcodeplace/overdeck',
  prNumber: 902,
  prUrl: 'https://github.com/alexcodeplace/overdeck/pull/902',
  branch: 'integration/batch-train-3',
  createdAt: '2026-07-19T09:00:00.000Z',
  members: [79],
  state: 'gating',
  gateRun: {
    ...CI_TRAIN.gateRun!,
    id: 654321,
    status: 'in_progress',
    conclusion: null,
    jobs: [
      {
        ...CI_TRAIN.gateRun!.jobs[1]!,
        id: 43,
        name: 'Gate',
        status: 'in_progress',
        conclusion: null,
        completedAt: null,
        infraFlag: null,
      },
    ],
  },
  blockedBy: 'Gate',
  infraFlag: null,
}

export const CI_TRAIN_REPO_COMPLETENESS = [
  {
    repo: CI_TRAIN.repo,
    queueComplete: true,
    historyComplete: true,
    trainRunsComplete: true,
    runnersComplete: true,
    refsComplete: true,
    prsComplete: true,
    trainsComplete: true,
  },
  {
    repo: CI_TRAIN_SECONDARY.repo,
    queueComplete: true,
    historyComplete: true,
    trainRunsComplete: true,
    runnersComplete: true,
    refsComplete: true,
    prsComplete: true,
    trainsComplete: true,
  },
]

export const CI_PR_QUEUE: PrQueueItem[] = [
  {
    repo: 'alexcodeplace/multideal',
    number: 79,
    title: 'Prevent duplicate payment capture',
    url: 'https://github.com/alexcodeplace/multideal/pull/79',
    branch: 'fix/payment-capture',
    headRefOid: 'oid-multideal-79-a',
    createdAt: '2026-07-18T08:00:00.000Z',
    ageH: 25,
    mergeable: 'CONFLICTING',
    staleVsMain: true,
    checksComplete: true,
    checks: [
      { name: 'Unit tests', bucket: 'fail' },
      { name: 'Typecheck', bucket: 'pass' },
    ],
    trainPr: 901,
  },
  {
    repo: 'alexcodeplace/overdeck',
    number: 79,
    title: 'Expose CI train state',
    url: 'https://github.com/alexcodeplace/overdeck/pull/79',
    branch: 'feat/ci-trains',
    headRefOid: 'oid-overdeck-79-a',
    createdAt: '2026-07-18T08:00:00.000Z',
    ageH: 25,
    mergeable: 'UNKNOWN',
    staleVsMain: null,
    checksComplete: false,
    checks: [{ name: 'Browser build', bucket: 'pass' }],
    trainPr: null,
  },
  {
    repo: 'alexcodeplace/multideal',
    number: 74,
    title: 'Harden webhook replay guard',
    url: 'https://github.com/alexcodeplace/multideal/pull/74',
    branch: 'fix/webhook-replay',
    headRefOid: 'oid-multideal-74-a',
    createdAt: '2026-07-19T05:00:00.000Z',
    ageH: 4,
    mergeable: 'MERGEABLE',
    staleVsMain: false,
    checksComplete: true,
    checks: [{ name: 'Integration', bucket: 'pending' }],
    trainPr: null,
  },
]

export const CI_PR_QUEUE_REPOS: PrQueueRepoCompleteness[] = [
  { repo: 'alexcodeplace/multideal', prsComplete: true },
  { repo: 'alexcodeplace/overdeck', prsComplete: true },
]

export const CI_RUNNERS: RunnerFleetRunner[] = [
  {
    id: 7,
    name: 'debian-1',
    status: 'online',
    busy: true,
    currentJob: 'Integration database lanes',
    currentRepo: 'alexcodeplace/multideal',
    currentKnown: true,
  },
  {
    id: 8,
    name: 'debian-2',
    status: 'online',
    busy: false,
    currentJob: null,
    currentRepo: null,
    currentKnown: true,
  },
  {
    id: 9,
    name: 'runner-current-unknown',
    status: 'online',
    busy: true,
    currentJob: 'Unverified job',
    currentRepo: 'unverified/repo',
    currentKnown: false,
  },
]

export const CI_RUNNERS_ALL_BUSY: RunnerFleetRunner[] = CI_RUNNERS.filter(
  (runner) => runner.busy,
)

export const CI_RUNNERS_ALL_OFFLINE: RunnerFleetRunner[] = [
  {
    id: 10,
    name: 'offline-runner',
    status: 'offline',
    busy: true,
    currentJob: null,
    currentRepo: null,
    currentKnown: false,
  },
]

export const CI_AMPLIFICATION_GATES: AmplificationGate[] = Array.from(
  { length: 12 },
  (_, index) => ({
    runId: 112 - index,
    computeMin: 10 + index,
    wallMin: (10 + index) * 3,
  }),
)

export const CI_AMPLIFICATION_INVALID_GATES: AmplificationGate[] = [
  { runId: 205, computeMin: 0, wallMin: 90 },
  { runId: 204, computeMin: Number.NaN, wallMin: 45 },
  { runId: 203, computeMin: 10, wallMin: 20 },
  { runId: 202, computeMin: 10, wallMin: 30 },
  { runId: 201, computeMin: 10, wallMin: 80 },
]

export const CI_AMPLIFICATION_RESTART: AmplificationSparkProps = {
  repo: 'alexcodeplace/multideal',
  gates: [],
  historyComplete: false,
  warmedAt: null,
}
