// Raw parsed frame from the TCP stream
export interface RawFrame {
  flag: number;
  func: number;
  payload: Uint8Array;
}

// Setup configuration sent to device (maps to setupReq_t)
export interface SetupConfig {
  channelNum: number;       // 1–6
  sendFreq: number;         // transmitter freq code
  sampleFreq: number;       // ADC sample freq code
  sampleDepth: number;      // points per cycle (uint16)
  accNum: number;           // stack count (14-bit, 0–16383)
  negAcc456: boolean;       // reverse stack ch4-6
  negAcc123: boolean;       // reverse stack ch1-3
  ampRatio: number;         // gain code
  dataChannel: number;      // 0x01=USB 0x02=P900 0x03=WiFi
  compRes: number;          // compensation resistor
  secondSampleFreq: number; // secondary sample freq code
  compDisableDelay: number; // delay × 50μs (uint16)
  sourceMode: number;       // source mode code
  batteryVoltageMin: number;// min battery voltage (uint16)
  filePrefix: string;       // SD file prefix, max 15 chars
}

// Generic ACK from device
export interface AckPacket {
  result: number;  // 0x01=OK 0x02=FAIL
  reason: string;
}

// Measurement metadata from device (first 54 bytes of 0x85 payload)
export interface MeasurementMeta {
  devId: number;
  utc: number;          // seconds since epoch
  longitude: number;    // degrees
  latitude: number;     // degrees
  altitude: number;     // meters
  height: number;
  sdStatus: number;     // bit[3:0]
  gpsStatus: number;    // bit[7:4] >> 4
  ampRatio: number;
  roll: number;         // degrees
  pitch: number;        // degrees
  yaw: number;          // degrees
  channelNum: number;
  current: number;      // peak TX current (raw)
  temperature: number;  // raw ADC
  batteryVolt: number;  // raw ADC
  sourceMode: number;
}

// A complete measurement frame (single or multi-source)
export interface MeasurementFrame {
  meta: MeasurementMeta;
  // channelNum × sampleDepth matrix, indexed [channel][sample]
  adcRaw: Int32Array[];
  // Converted to μV: raw / accNum / gain
  adcUV: Float64Array[];
  accNum:        number;  // stack count at time of reception
  gain:          number;  // gain multiplier at time of reception
  sampleFreqCode:number;  // ADC sample frequency code (from SetupConfig)
  timestamp:     number;  // local ms timestamp when received
  frameId:       number;  // sequential frame counter
}

// Split frame session state
export interface SplitFrameSession {
  active: boolean;
  frameNo: number;
  chunks: Uint8Array[];
}

// Connection state values
export type ConnectionStatus =
  | 'disconnected'
  | 'connecting'
  | 'connected'
  | 'reconnecting'
  | 'error';

// Device running state
export type DeviceStatus = 'idle' | 'running' | 'single';

// ── Gate config (profile screen, persisted in deviceStore) ─────────────────
export type GateSpacing = 'log' | 'linear';

export interface GateConfig {
  tStart:  number;   // μs
  tEnd:    number;   // μs
  count:   number;
  spacing: GateSpacing;
}
