import type { BeaconVibrationSample } from '../model/types'

type VibrationMode = 'idle' | 'engine' | 'moving' | 'load'

type VibrationSegment = {
  mode: VibrationMode
  startMinute: number
  endMinute: number
}

type MachineVibrationProfile = {
  mac: string
  idle: [number, number]
  engine: [number, number]
  moving: [number, number]
  load: [number, number]
  segments: VibrationSegment[]
}

const baseDayUtc = Date.UTC(2026, 6, 9)
const dayMs = 24 * 60 * 60 * 1000
const minuteMs = 60 * 1000
const sampleStepMinutes = 15
const sampleDurationSeconds = sampleStepMinutes * 60

const profiles: MachineVibrationProfile[] = [
  {
    mac: 'E4:5F:01:AA:10:01',
    idle: [0.015, 0.055],
    engine: [0.11, 0.24],
    moving: [0.38, 0.95],
    load: [0.7, 1.15],
    segments: [
      { mode: 'idle', startMinute: 45, endMinute: 95 },
      { mode: 'engine', startMinute: 95, endMinute: 125 },
      { mode: 'moving', startMinute: 125, endMinute: 280 },
      { mode: 'idle', startMinute: 280, endMinute: 335 },
      { mode: 'engine', startMinute: 335, endMinute: 365 },
      { mode: 'load', startMinute: 365, endMinute: 540 },
      { mode: 'moving', startMinute: 540, endMinute: 650 },
      { mode: 'engine', startMinute: 650, endMinute: 690 },
      { mode: 'idle', startMinute: 690, endMinute: 750 },
    ],
  },
  {
    mac: 'E4:5F:01:AA:10:09',
    idle: [0.02, 0.075],
    engine: [0.18, 0.38],
    moving: [0.55, 1.18],
    load: [0.95, 1.68],
    segments: [
      { mode: 'idle', startMinute: 35, endMinute: 80 },
      { mode: 'engine', startMinute: 80, endMinute: 120 },
      { mode: 'load', startMinute: 120, endMinute: 245 },
      { mode: 'idle', startMinute: 245, endMinute: 320 },
      { mode: 'engine', startMinute: 320, endMinute: 360 },
      { mode: 'load', startMinute: 360, endMinute: 535 },
      { mode: 'moving', startMinute: 535, endMinute: 630 },
      { mode: 'engine', startMinute: 630, endMinute: 680 },
      { mode: 'idle', startMinute: 680, endMinute: 735 },
    ],
  },
  {
    mac: 'E4:5F:01:AA:10:10',
    idle: [0.006, 0.035],
    engine: [0.052, 0.155],
    moving: [0.19, 0.58],
    load: [0.34, 0.78],
    segments: [
      { mode: 'idle', startMinute: 70, endMinute: 110 },
      { mode: 'engine', startMinute: 110, endMinute: 135 },
      { mode: 'moving', startMinute: 135, endMinute: 225 },
      { mode: 'idle', startMinute: 225, endMinute: 285 },
      { mode: 'engine', startMinute: 285, endMinute: 315 },
      { mode: 'moving', startMinute: 315, endMinute: 430 },
      { mode: 'idle', startMinute: 430, endMinute: 520 },
      { mode: 'engine', startMinute: 520, endMinute: 548 },
      { mode: 'load', startMinute: 548, endMinute: 625 },
      { mode: 'moving', startMinute: 625, endMinute: 700 },
    ],
  },
]

function getValueInRange(
  range: [number, number],
  machineIndex: number,
  dayOffset: number,
  sampleIndex: number,
) {
  const [min, max] = range
  const seed = (
    machineIndex * 31 +
    Math.abs(dayOffset) * 17 +
    sampleIndex * 11
  ) % 100
  const ratio = seed / 100
  const wave = Math.sin((sampleIndex + machineIndex * 3) / 3) * 0.08
  const value = min + (max - min) * Math.min(1, Math.max(0, ratio + wave))

  return Number(value.toFixed(3))
}

function buildSamples() {
  let nextId = 1
  const samples: BeaconVibrationSample[] = []

  profiles.forEach((profile, machineIndex) => {
    for (let dayOffset = -4; dayOffset <= 0; dayOffset += 1) {
      const shift = ((machineIndex + Math.abs(dayOffset)) % 3) * 5

      profile.segments.forEach((segment) => {
        for (
          let minute = segment.startMinute + shift;
          minute < segment.endMinute + shift;
          minute += sampleStepMinutes
        ) {
          const timeMillis = baseDayUtc + dayOffset * dayMs + minute * minuteMs

          samples.push({
            id: nextId,
            mac: profile.mac,
            vibration: getValueInRange(
              profile[segment.mode],
              machineIndex,
              dayOffset,
              nextId,
            ),
            duration: sampleDurationSeconds,
            timeMillis,
          })

          nextId += 1
        }
      })
    }
  })

  return samples
}

export const mockBeaconVibrationSamples: BeaconVibrationSample[] = buildSamples()
