/**
 * Type extensions for Hardhat
 */

import "hardhat/types/config";
import "hardhat/types/runtime";

import { SynorProvider } from "./provider";
import { SynorDeployer } from "./deployer";
import { SynorNetwork } from "./network";

declare module "hardhat/types/config" {
  export interface HardhatUserConfig {
    synor?: SynorConfig;
  }

  export interface HardhatConfig {
    synor: SynorConfig;
  }
}

declare module "hardhat/types/runtime" {
  export interface HardhatRuntimeEnvironment {
    synor: SynorRuntime;
  }
}

/**
 * Synor plugin configuration
 */
export interface SynorConfig {
  /** Default Synor network to use */
  defaultNetwork?: "mainnet" | "testnet" | "devnet";
  /** Gas price setting */
  gasPrice?: "auto" | number;
  /** Gas limit for transactions */
  gasLimit?: number;
  /** Number of confirmations to wait */
  confirmations?: number;
  /** Transaction timeout in ms */
  timeout?: number;
  /** Auto-verify contracts after deployment */
  verifyContracts?: boolean;
  /** Use quantum-safe signatures */
  quantumSafe?: boolean;
}

/**
 * Synor runtime interface
 */
export interface SynorRuntime {
  /** Synor provider for RPC calls */
  provider: SynorProvider;
  /** Contract deployer */
  deployer: SynorDeployer;
  /** Network utilities */
  network: SynorNetwork;
  /** Get balance of address */
  getBalance(address: string): Promise<bigint>;
  /** Get current block number */
  getBlockNumber(): Promise<number>;
  /** Send a transaction */
  sendTransaction(tx: TransactionRequest): Promise<TransactionResponse>;
}

/**
 * Transaction request
 */
export interface TransactionRequest {
  to?: string;
  from?: string;
  value?: bigint | string;
  data?: string;
  gasLimit?: number;
  gasPrice?: bigint;
  nonce?: number;
}

/**
 * Transaction response
 */
export interface TransactionResponse {
  hash: string;
  from: string;
  to?: string;
  value: bigint;
  gasLimit: bigint;
  gasPrice: bigint;
  nonce: number;
  data: string;
  blockNumber?: number;
  blockHash?: string;
  timestamp?: number;
  confirmations: number;
  wait(confirmations?: number): Promise<TransactionReceipt>;
}

/**
 * Transaction receipt
 */
export interface TransactionReceipt {
  hash: string;
  blockNumber: number;
  blockHash: string;
  transactionIndex: number;
  from: string;
  to?: string;
  contractAddress?: string;
  gasUsed: bigint;
  status: number;
  logs: Log[];
  events: Event[];
}

/**
 * Log entry
 */
export interface Log {
  blockNumber: number;
  blockHash: string;
  transactionIndex: number;
  transactionHash: string;
  logIndex: number;
  address: string;
  topics: string[];
  data: string;
}

/**
 * Parsed event
 */
export interface Event extends Log {
  event?: string;
  args?: any[];
}

/**
 * Network info
 */
export interface NetworkInfo {
  name: string;
  chainId: number;
  blockHeight: number;
  latestBlockHash: string;
  gasPrice: string;
  protocolVersion: string;
  consensus: string;
  quantumSafe: boolean;
}

/**
 * Deployed contract
 */
export interface DeployedContract {
  address: string;
  deployTransaction: TransactionResponse;
  interface: any;
  connect(signer: any): DeployedContract;
}
