import type {
  MachineStateCode,
  MachineTelemetryInterval,
} from '../model/types'

type TelemetryProfile = 'haul' | 'loader' | 'service' | 'no-data'

type ScheduleItem = {
  state: MachineStateCode
  startMinute: number
  endMinute: number
}

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

const profileSchedules: Record<TelemetryProfile, ScheduleItem[]> = {
  haul: [
    { state: 'NO_DATA', startMinute: 0, endMinute: 35 },
    { state: 'IDLE', startMinute: 35, endMinute: 70 },
    { state: 'ENGINE_ON', startMinute: 70, endMinute: 92 },
    { state: 'MOVING', startMinute: 92, endMinute: 255 },
    { state: 'IDLE', startMinute: 255, endMinute: 305 },
    { state: 'ENGINE_ON', startMinute: 305, endMinute: 326 },
    { state: 'MOVING', startMinute: 326, endMinute: 514 },
    { state: 'IDLE', startMinute: 514, endMinute: 560 },
    { state: 'MOVING', startMinute: 560, endMinute: 670 },
    { state: 'ENGINE_ON', startMinute: 670, endMinute: 692 },
    { state: 'IDLE', startMinute: 692, endMinute: 730 },
  ],
  loader: [
    { state: 'NO_DATA', startMinute: 0, endMinute: 25 },
    { state: 'IDLE', startMinute: 25, endMinute: 86 },
    { state: 'ENGINE_ON', startMinute: 86, endMinute: 128 },
    { state: 'MOVING', startMinute: 128, endMinute: 238 },
    { state: 'ENGINE_ON', startMinute: 238, endMinute: 286 },
    { state: 'MOVING', startMinute: 286, endMinute: 390 },
    { state: 'IDLE', startMinute: 390, endMinute: 452 },
    { state: 'ENGINE_ON', startMinute: 452, endMinute: 512 },
    { state: 'MOVING', startMinute: 512, endMinute: 645 },
    { state: 'IDLE', startMinute: 645, endMinute: 720 },
  ],
  service: [
    { state: 'NO_DATA', startMinute: 0, endMinute: 45 },
    { state: 'IDLE', startMinute: 45, endMinute: 170 },
    { state: 'ENGINE_ON', startMinute: 170, endMinute: 198 },
    { state: 'MOVING', startMinute: 198, endMinute: 305 },
    { state: 'IDLE', startMinute: 305, endMinute: 438 },
    { state: 'ENGINE_ON', startMinute: 438, endMinute: 476 },
    { state: 'MOVING', startMinute: 476, endMinute: 552 },
    { state: 'IDLE', startMinute: 552, endMinute: 710 },
  ],
  'no-data': [
    { state: 'NO_DATA', startMinute: 0, endMinute: 720 },
  ],
}

const machineProfiles: Record<number, TelemetryProfile> = {
  1: 'haul',
  2: 'loader',
  3: 'service',
  4: 'no-data',
  5: 'service',
  6: 'service',
  7: 'loader',
  8: 'service',
  9: 'no-data',
  10: 'haul',
  11: 'service',
  12: 'loader',
}

function toIso(dayOffset: number, minute: number) {
  return new Date(baseDayUtc + dayOffset * dayMs + minute * minuteMs).toISOString()
}

function getMinuteVariation(machineId: number, dayOffset: number) {
  return ((machineId * 17 + Math.abs(dayOffset) * 13) % 25) - 12
}

function buildIntervals() {
  let nextId = 1
  const intervals: MachineTelemetryInterval[] = []

  Object.entries(machineProfiles).forEach(([machineIdValue, profile]) => {
    const machineId = Number(machineIdValue)

    for (let dayOffset = -6; dayOffset <= 0; dayOffset += 1) {
      const variation = getMinuteVariation(machineId, dayOffset)

      profileSchedules[profile].forEach((item) => {
        const startMinute = Math.max(0, item.startMinute + variation)
        const endMinute = Math.min(24 * 60, item.endMinute + variation)

        if (endMinute <= startMinute) return

        intervals.push({
          id: nextId,
          machineId,
          state: item.state,
          startedAt: toIso(dayOffset, startMinute),
          endedAt: toIso(dayOffset, endMinute),
          durationSeconds: (endMinute - startMinute) * 60,
        })

        nextId += 1
      })
    }
  })

  return intervals
}

export const mockMachineTelemetryIntervals: MachineTelemetryInterval[] = buildIntervals()
