import { IRepository } from '../../../../core/interfaces/IRepository';
import { 
  UserWallet, 
  WalletTransaction, 
  TransactionType, 
  TransactionStatus,
  TransactionSource,
  WalletBalanceSummary,
  TransactionFilter,
  WalletStatistics
} from '../../types/wallet.types';

export interface IWalletRepository extends IRepository<UserWallet> {
  // Basic CRUD operations inherited from IRepository
  
  // Wallet-specific queries
  findByUserId(userId: string): Promise<UserWallet | null>;
  findByUserIds(userIds: string[]): Promise<UserWallet[]>;
  
  // Balance operations
  updateBalance(
    walletId: string, 
    amount: number, 
    type: 'credit' | 'debit'
  ): Promise<UserWallet>;
  
  getBalanceSummary(userId: string): Promise<WalletBalanceSummary>;
  
  // Freeze/Unfreeze operations
  freezeWallet(walletId: string, reason: string): Promise<void>;
  unfreezeWallet(walletId: string): Promise<void>;
  
  // Wallet statistics
  getWalletStatistics(userId: string): Promise<WalletStatistics>;
  
  // Batch operations
  updateBalancesBatch(updates: Array<{
    walletId: string;
    amount: number;
    type: 'credit' | 'debit';
  }>): Promise<void>;
}

export interface IWalletTransactionRepository extends IRepository<WalletTransaction> {
  // Transaction-specific queries
  findByWalletId(
    walletId: string, 
    limit?: number, 
    offset?: number
  ): Promise<WalletTransaction[]>;
  
  findByUserId(
    userId: string, 
    limit?: number, 
    offset?: number
  ): Promise<WalletTransaction[]>;
  
  findByFilter(filter: TransactionFilter): Promise<{
    transactions: WalletTransaction[];
    total: number;
  }>;
  
  // Transaction status operations
  updateTransactionStatus(
    transactionId: string, 
    status: TransactionStatus,
    processedAt?: Date,
    failureReason?: string
  ): Promise<WalletTransaction>;
  
  // Batch operations
  createBatch(
    transactions: Omit<WalletTransaction, 'id' | 'createdAt' | 'updatedAt'>[]
  ): Promise<WalletTransaction[]>;
  
  updateStatusBatch(
    transactionIds: string[], 
    status: TransactionStatus
  ): Promise<void>;
  
  // Financial queries
  getUserTransactionSummary(
    userId: string, 
    startDate: Date, 
    endDate: Date
  ): Promise<{
    totalCredits: number;
    totalDebits: number;
    transactionCount: number;
    netAmount: number;
  }>;
  
  // Reconciliation
  findPendingTransactions(olderThan?: Date): Promise<WalletTransaction[]>;
  
  findTransactionsByReference(
    referenceId: string, 
    referenceType: string
  ): Promise<WalletTransaction[]>;
  
  // Transfer operations
  findTransferPair(transferId: string): Promise<{
    debitTransaction: WalletTransaction;
    creditTransaction: WalletTransaction;
  } | null>;
  
  // Audit and reporting
  getTransactionTrends(
    startDate: Date, 
    endDate: Date, 
    groupBy: 'day' | 'week' | 'month'
  ): Promise<Array<{
    period: string;
    totalTransactions: number;
    totalAmount: number;
    creditAmount: number;
    debitAmount: number;
  }>>;
  
  // Validation
  validateTransactionIntegrity(
    walletId: string, 
    startDate?: Date, 
    endDate?: Date
  ): Promise<{
    isValid: boolean;
    expectedBalance: number;
    actualBalance: number;
    discrepancy: number;
  }>;
}