export type ChainType =
  | "eth"
  | "ethereum"
  | "bnb"
  | "solana"
  | "tron"
  | "base"
  | "bsc"
  | string;

export interface WalletMappingInput {
  chain_type: string;
  address: string;
  wallet_id?: string | null;
  derivation_index?: number | null;
  organization_id?: string | null;
  branch_id?: string | null;
  terminal_id?: string | null;
  payment_id?: string | null;
  supported_asset_id?: string | null;
  network?: string | null;
}

export interface TrackedAddressRegistration {
  user_id: number;
  wallet: WalletMappingInput;
}

export interface TrackedAddressRecord {
  address: string;
  normalized_address: string;
  user_id: number;
  chain_type: string;
  wallet_id?: string | null;
  webhook_id: string;
  shard_key: string;
  created_at: string;
  updated_at: string;
}

export interface WebhookShardDefinition {
  webhook_id: string;
  capacity?: number;
  label?: string | null;
}

export interface WebhookShardMetadata {
  shard_key: string;
  webhook_id: string;
  chain_type: string;
  capacity: number;
  address_count: number;
  addresses: string[];
  label?: string | null;
  last_synced_at?: string | null;
}

export interface AddressTrackingStore {
  version: 1;
  addresses: Record<string, TrackedAddressRecord>;
  webhooks: Record<string, WebhookShardMetadata>;
}

export interface ReconciliationCursor {
  chain_type: string;
  from_block: string;
  updated_at: string;
}

export interface ReconciliationStore {
  version: 1;
  cursors: Record<string, ReconciliationCursor>;
}

export interface AddressTrackingResult {
  tracked: number;
  skipped: number;
  overflow: number;
  assignments: TrackedAddressRecord[];
}

export interface UserMapping {
  user_id: number;
  wallets: WalletMappingInput[];
}

export interface DepositEvent {
  source: string;
  status: "confirmed";
  idempotency_key: string;
  user_id: number;
  wallet_address: string;
  amount: string;
  amount_sats?: number | null;
  asset: string;
  tx_hash: string;
  payment_hash?: string | null;
  provider_reference?: string | null;
  transaction_status?: string | null;
  wallet_currency?: string | null;
  network?: string | null;
  block_number: number | null;
  occurred_at: string;
  log_index?: string | null;
  from_address?: string | null;
  to_address?: string | null;
  asset_address?: string | null;
  chain_type?: string | null;
  wallet_id?: string | null;
  organization_id?: string | null;
  branch_id?: string | null;
  terminal_id?: string | null;
  payment_id?: string | null;
  supported_asset_id?: string | null;
  derivation_index?: number | null;
  raw_payload: Record<string, unknown>;
}

export interface RegisterWalletsInput {
  userId: number;
  wallets: WalletMappingInput[];
}

export interface AlchemyAddressActivity {
  blockNum?: string;
  hash?: string;
  fromAddress?: string;
  toAddress?: string;
  value?: number | string | null;
  erc721TokenId?: string | null;
  erc1155Metadata?: Array<{
    tokenId?: string | null;
    value?: string | null;
  }> | null;
  asset?: string | null;
  category?: string | null;
  rawContract?: {
    rawValue?: string | null;
    address?: string | null;
    decimals?: number | null;
  };
  typeTraceAddress?: string | null;
  log?: {
    address?: string | null;
    topics?: string[];
    data?: string | null;
    blockNumber?: string | null;
    transactionHash?: string | null;
    transactionIndex?: string | null;
    blockHash?: string | null;
    logIndex?: string | null;
    removed?: boolean;
  };
}

export interface AlchemyWebhookPayload {
  webhookId: string;
  id: string;
  createdAt: string;
  type: "ADDRESS_ACTIVITY" | string;
  event?: {
    network?: string;
    activity?: AlchemyAddressActivity[];
    transaction?: Array<Record<string, unknown>>;
    slot?: number;
  };
}

export interface AlchemyWebhookHandlerResult {
  accepted: number;
  duplicates: number;
  ignored: number;
  unsupported: number;
  reorgEvents: number;
}

export interface LaravelDepositPayload {
  user_id: number;
  idempotency_key: string;
  wallet_address: string;
  amount: string;
  asset: string;
  tx_hash: string;
  block_number: number | null;
  asset_address?: string | null;
  from_address?: string | null;
  to_address?: string | null;
  chain_type?: string | null;
  network?: string | null;
  wallet_id?: string | null;
  organization_id?: string | null;
  branch_id?: string | null;
  terminal_id?: string | null;
  payment_id?: string | null;
  supported_asset_id?: string | null;
  derivation_index?: number | null;
  occurred_at?: string | null;
  raw_payload?: Record<string, unknown>;
}

export interface LaravelBlinkDepositPayload {
  provider: "blink";
  network: "bitcoin_lightning";
  asset: "BTC";
  user_id: number;
  amount_sats: number;
  payment_hash: string;
  provider_reference?: string | null;
  status: "confirmed";
  raw_payload?: Record<string, unknown>;
}

export interface AlchemyAssetTransfer {
  blockNum?: string;
  uniqueId?: string;
  hash?: string;
  from?: string;
  to?: string;
  value?: number | string | null;
  asset?: string | null;
  category?: string | null;
  rawContract?: {
    value?: string | null;
    address?: string | null;
    decimal?: string | null;
  };
  metadata?: {
    blockTimestamp?: string;
  };
  erc721TokenId?: string | null;
  erc1155Metadata?: Array<{
    tokenId?: string | null;
    value?: string | null;
  }> | null;
}

export interface ReconciliationResult {
  scanned: number;
  missing: number;
  duplicates: number;
  unsupported: number;
  overflow: number;
}
