import { createContext, useContext } from 'react';

export interface MeshCorePacketRecord {
  timestamp: Date;
  hash: string;
  nodeType: number;
  payloadType: number;
  routeType: number;
  version: number;
  path: Uint8Array;
  raw: Uint8Array;
  decodedPayload?: unknown;
  payloadSummary: string;
  radioName?: string;
}

export interface MeshCoreGroupChatRecord {
  hash: string;
  timestamp: Date;
  channel: string;
  sender: string;
  message: string;
}

export interface MeshCoreNodePoint {
  nodeId: string;
  nodeType: number;
  packetCount: number;
  latitude: number;
  longitude: number;
}

export interface MeshCoreDataContextValue {
  packets: MeshCorePacketRecord[];
  groupChats: MeshCoreGroupChatRecord[];
  mapPoints: MeshCoreNodePoint[];
  streamReady: boolean;
}

export const MeshCoreDataContext = createContext<MeshCoreDataContextValue | null>(null);

export const useMeshCoreData = (): MeshCoreDataContextValue => {
  const ctx = useContext(MeshCoreDataContext);
  if (!ctx) {
    throw new Error('useMeshCoreData must be used within MeshCoreDataProvider');
  }
  return ctx;
};

export default MeshCoreDataContext;
