import { createContext, useContext } from 'react'
import type { ReactNode } from 'react'
import { useTransactionHistory } from '../hooks/useTransactionHistory'
import type { TransactionRecord, TransactionType } from '../hooks/useTransactionHistory'

interface TransactionContextType {
  transactions: TransactionRecord[]
  addTransaction: (tx: Omit<TransactionRecord, 'id' | 'timestamp'>) => string
  updateTransaction: (id: string, updates: Partial<TransactionRecord>) => void
  clearHistory: () => void
}

const TransactionContext = createContext<TransactionContextType | null>(null)

export function TransactionProvider({ children }: { children: ReactNode }) {
  const history = useTransactionHistory()

  return (
    <TransactionContext.Provider value={history}>
      {children}
    </TransactionContext.Provider>
  )
}

export function useTransactions() {
  const context = useContext(TransactionContext)
  if (!context) {
    throw new Error('useTransactions must be used within TransactionProvider')
  }
  return context
}

export type { TransactionType, TransactionRecord }
