/**
 * Synor Compute SDK Types
 */

/** Supported processor types */
export type ProcessorType =
  | 'cpu'
  | 'gpu'
  | 'tpu'
  | 'npu'
  | 'lpu'
  | 'fpga'
  | 'wasm'
  | 'webgpu';

/** Precision levels for compute operations */
export type Precision = 'fp64' | 'fp32' | 'fp16' | 'bf16' | 'int8' | 'int4';

/** Balancing strategy for job scheduling */
export type BalancingStrategy =
  | 'speed'    // Minimize execution time
  | 'cost'     // Minimize cost (prefer consumer devices)
  | 'energy'   // Minimize energy consumption
  | 'latency'  // Minimize latency (for inference)
  | 'balanced'; // Balance all factors

/** Task priority levels */
export type TaskPriority = 'critical' | 'high' | 'normal' | 'background';

/** Job status */
export type JobStatus =
  | 'pending'
  | 'queued'
  | 'running'
  | 'completed'
  | 'failed'
  | 'cancelled';

/** SDK configuration */
export interface SynorConfig {
  /** API key for authentication */
  apiKey: string;
  /** API endpoint (defaults to production) */
  endpoint?: string;
  /** Default balancing strategy */
  strategy?: BalancingStrategy;
  /** Default precision */
  precision?: Precision;
  /** Request timeout in milliseconds */
  timeout?: number;
  /** Enable debug logging */
  debug?: boolean;
}

/** Tensor shape descriptor */
export interface TensorShape {
  /** Dimensions */
  dims: number[];
  /** Data type */
  dtype: Precision;
}

/** Tensor data (can be typed array or nested array) */
export type TensorData =
  | Float64Array
  | Float32Array
  | Int32Array
  | Int16Array
  | Int8Array
  | Uint8Array
  | number[]
  | number[][];

/** Matrix multiplication options */
export interface MatMulOptions {
  /** First matrix */
  a: TensorData;
  /** Second matrix */
  b: TensorData;
  /** Precision */
  precision?: Precision;
  /** Preferred processor type */
  processor?: ProcessorType;
  /** Task priority */
  priority?: TaskPriority;
}

/** Convolution options */
export interface Conv2dOptions {
  /** Input tensor [batch, channels, height, width] */
  input: TensorData;
  /** Kernel/filter tensor */
  kernel: TensorData;
  /** Stride */
  stride?: number | [number, number];
  /** Padding */
  padding?: number | [number, number];
  /** Precision */
  precision?: Precision;
}

/** Attention options */
export interface AttentionOptions {
  /** Query tensor */
  query: TensorData;
  /** Key tensor */
  key: TensorData;
  /** Value tensor */
  value: TensorData;
  /** Number of attention heads */
  numHeads: number;
  /** Use flash attention */
  flash?: boolean;
  /** Precision */
  precision?: Precision;
}

/** Inference options */
export interface InferenceOptions {
  /** Model name or CID */
  model: string;
  /** Input prompt or data */
  input: string | TensorData;
  /** Maximum tokens to generate */
  maxTokens?: number;
  /** Temperature for sampling */
  temperature?: number;
  /** Top-k sampling */
  topK?: number;
  /** Balancing strategy */
  strategy?: BalancingStrategy;
  /** Stream responses */
  stream?: boolean;
}

/** Job submission result */
export interface JobResult<T = unknown> {
  /** Job ID */
  jobId: string;
  /** Job status */
  status: JobStatus;
  /** Result data (if completed) */
  data?: T;
  /** Error message (if failed) */
  error?: string;
  /** Execution metrics */
  metrics?: JobMetrics;
}

/** Job execution metrics */
export interface JobMetrics {
  /** Total execution time (ms) */
  executionTimeMs: number;
  /** Queue wait time (ms) */
  queueTimeMs: number;
  /** Processor type used */
  processorType: ProcessorType;
  /** Processor ID */
  processorId: string;
  /** Estimated FLOPS */
  flops: number;
  /** Memory used (bytes) */
  memoryBytes: number;
  /** Cost in microcredits */
  costMicro: number;
  /** Energy consumed (mJ) */
  energyMj: number;
}

/** Spot market order */
export interface SpotOrder {
  /** Order ID */
  orderId: string;
  /** Order type */
  type: 'bid' | 'ask';
  /** Processor type */
  processorType: ProcessorType;
  /** Price in microcredits per GFLOP */
  priceMicro: number;
  /** Quantity (GFLOPS) */
  quantity: number;
  /** Region preference */
  region?: string;
}

/** Compute pricing info */
export interface PricingInfo {
  /** Processor type */
  processorType: ProcessorType;
  /** Current spot price (microcredits/GFLOP) */
  spotPrice: number;
  /** 24h average price */
  avgPrice24h: number;
  /** AWS equivalent price */
  awsEquivalent: number;
  /** Savings percentage vs AWS */
  savingsPercent: number;
}
