/**
 * Synor Contract SDK Types
 */

/** Contract SDK configuration */
export interface ContractConfig {
  /** API key for authentication */
  apiKey: string;
  /** API endpoint (defaults to production) */
  endpoint?: string;
  /** Request timeout in milliseconds */
  timeout?: number;
  /** Number of retries for failed requests */
  retries?: number;
  /** Enable debug logging */
  debug?: boolean;
  /** Default gas limit */
  defaultGasLimit?: string;
  /** Default gas price */
  defaultGasPrice?: string;
}

/** ABI entry types */
export type AbiEntryType = 'function' | 'constructor' | 'event' | 'error' | 'fallback' | 'receive';

/** ABI input/output parameter */
export interface AbiParameter {
  /** Parameter name */
  name: string;
  /** Parameter type */
  type: string;
  /** Indexed (for events) */
  indexed?: boolean;
  /** Component types (for tuples) */
  components?: AbiParameter[];
  /** Internal type */
  internalType?: string;
}

/** ABI entry */
export interface AbiEntry {
  /** Entry type */
  type: AbiEntryType;
  /** Function/event name */
  name?: string;
  /** Input parameters */
  inputs?: AbiParameter[];
  /** Output parameters */
  outputs?: AbiParameter[];
  /** State mutability */
  stateMutability?: 'pure' | 'view' | 'nonpayable' | 'payable';
  /** Anonymous (for events) */
  anonymous?: boolean;
}

/** Contract ABI */
export type Abi = AbiEntry[];

/** Deploy contract options */
export interface DeployContractOptions {
  /** Compiled bytecode (hex) */
  bytecode: string;
  /** Contract ABI */
  abi?: Abi;
  /** Constructor arguments */
  args?: unknown[];
  /** Gas limit */
  gasLimit?: string;
  /** Gas price */
  gasPrice?: string;
  /** Value to send with deployment */
  value?: string;
  /** Salt for deterministic deployment (CREATE2) */
  salt?: string;
}

/** Deployment result */
export interface DeploymentResult {
  /** Deployed contract address */
  address: string;
  /** Deployment transaction hash */
  transactionHash: string;
  /** Block number of deployment */
  blockNumber: number;
  /** Gas used */
  gasUsed: string;
  /** Effective gas price */
  effectiveGasPrice: string;
}

/** Call contract options */
export interface CallContractOptions {
  /** Contract address */
  address: string;
  /** Method name */
  method: string;
  /** Method arguments */
  args?: unknown[];
  /** Contract ABI (required for encoding) */
  abi: Abi;
  /** Block number or 'latest' */
  blockNumber?: number | 'latest';
}

/** Send transaction options */
export interface SendContractOptions {
  /** Contract address */
  address: string;
  /** Method name */
  method: string;
  /** Method arguments */
  args?: unknown[];
  /** Contract ABI (required for encoding) */
  abi: Abi;
  /** Gas limit */
  gasLimit?: string;
  /** Gas price */
  gasPrice?: string;
  /** Value to send */
  value?: string;
}

/** Transaction result */
export interface TransactionResult {
  /** Transaction hash */
  transactionHash: string;
  /** Block number */
  blockNumber: number;
  /** Block hash */
  blockHash: string;
  /** Gas used */
  gasUsed: string;
  /** Effective gas price */
  effectiveGasPrice: string;
  /** Transaction status */
  status: 'success' | 'reverted';
  /** Logs emitted */
  logs: EventLog[];
  /** Return value (if any) */
  returnValue?: unknown;
  /** Revert reason (if reverted) */
  revertReason?: string;
}

/** Event log */
export interface EventLog {
  /** Log index */
  logIndex: number;
  /** Contract address */
  address: string;
  /** Raw topics */
  topics: string[];
  /** Raw data */
  data: string;
  /** Block number */
  blockNumber: number;
  /** Transaction hash */
  transactionHash: string;
}

/** Decoded event */
export interface DecodedEvent {
  /** Event name */
  name: string;
  /** Event signature */
  signature: string;
  /** Decoded arguments */
  args: Record<string, unknown>;
  /** Raw log */
  log: EventLog;
}

/** Event filter */
export interface EventFilter {
  /** Contract address */
  address: string;
  /** Event name (optional) */
  eventName?: string;
  /** Contract ABI (required for decoding) */
  abi?: Abi;
  /** From block */
  fromBlock?: number | 'earliest';
  /** To block */
  toBlock?: number | 'latest';
  /** Indexed filter values */
  filter?: Record<string, unknown>;
}

/** Event subscription callback */
export type EventCallback = (event: DecodedEvent) => void;

/** Event subscription */
export interface EventSubscription {
  /** Subscription ID */
  id: string;
  /** Unsubscribe */
  unsubscribe: () => Promise<void>;
}

/** Contract interface (from ABI) */
export interface ContractInterface {
  /** Contract ABI */
  abi: Abi;
  /** Function signatures */
  functions: Record<string, AbiEntry>;
  /** Event signatures */
  events: Record<string, AbiEntry>;
  /** Error signatures */
  errors: Record<string, AbiEntry>;
}

/** Encode call options */
export interface EncodeCallOptions {
  /** Method name */
  method: string;
  /** Arguments */
  args: unknown[];
  /** Contract ABI */
  abi: Abi;
}

/** Decode result options */
export interface DecodeResultOptions {
  /** Method name */
  method: string;
  /** Raw data to decode */
  data: string;
  /** Contract ABI */
  abi: Abi;
}

/** Gas estimation options */
export interface EstimateGasOptions {
  /** Contract address (for calls) */
  address?: string;
  /** Method name */
  method?: string;
  /** Arguments */
  args?: unknown[];
  /** Contract ABI */
  abi?: Abi;
  /** Bytecode (for deployment) */
  bytecode?: string;
  /** Value to send */
  value?: string;
  /** Sender address */
  from?: string;
}

/** Gas estimation result */
export interface GasEstimation {
  /** Estimated gas units */
  gasLimit: string;
  /** Current gas price */
  gasPrice: string;
  /** Estimated cost */
  estimatedCost: string;
}

/** Contract bytecode info */
export interface BytecodeInfo {
  /** Raw bytecode */
  bytecode: string;
  /** Deployed bytecode (without constructor) */
  deployedBytecode?: string;
  /** ABI */
  abi?: Abi;
  /** Source metadata */
  metadata?: {
    compiler: string;
    language: string;
    sources: string[];
  };
}

/** Contract verification options */
export interface VerifyContractOptions {
  /** Contract address */
  address: string;
  /** Source code */
  sourceCode: string;
  /** Compiler version */
  compilerVersion: string;
  /** Constructor arguments (hex) */
  constructorArguments?: string;
  /** Optimization enabled */
  optimization?: boolean;
  /** Optimization runs */
  optimizationRuns?: number;
  /** Contract name */
  contractName?: string;
}

/** Contract verification result */
export interface VerificationResult {
  /** Verification status */
  status: 'verified' | 'pending' | 'failed';
  /** Verification message */
  message: string;
  /** ABI (if verified) */
  abi?: Abi;
}

/** Multicall request */
export interface MulticallRequest {
  /** Contract address */
  address: string;
  /** Encoded call data */
  callData: string;
  /** Allow failure */
  allowFailure?: boolean;
}

/** Multicall result */
export interface MulticallResult {
  /** Success status */
  success: boolean;
  /** Return data */
  returnData: string;
  /** Decoded result (if ABI provided) */
  decoded?: unknown;
}

/** Storage read options */
export interface ReadStorageOptions {
  /** Contract address */
  address: string;
  /** Storage slot */
  slot: string;
  /** Block number */
  blockNumber?: number | 'latest';
}
