/**
 * [MOCK-FINANCE] Finance模块DataSource
 */

export interface Transaction {
  id: string;
  type: 'income' | 'expense' | 'refund' | 'fee';
  amount: number;
  currency: string;
  category: string;
  description: string;
  orderId?: string;
  status: 'pending' | 'completed' | 'failed';
  createdAt: string;
}

export interface ReconciliationRecord {
  id: string;
  platform: string;
  orderId: string;
  platformAmount: number;
  systemAmount: number;
  difference: number;
  status: 'matched' | 'mismatch' | 'pending';
  reconciledAt?: string;
  createdAt: string;
}

export interface IFinanceDataSource {
  fetchTransactions(params?: { type?: string; startDate?: string; endDate?: string }): Promise<Transaction[]>;
  createTransaction(data: Partial<Transaction>): Promise<Transaction>;
  
  fetchReconciliation(params?: { platform?: string; status?: string }): Promise<ReconciliationRecord[]>;
  reconcile(id: string, data: { action: 'approve' | 'reject'; note?: string }): Promise<ReconciliationRecord>;
  autoReconcile(platform: string): Promise<{ matched: number; mismatched: number }>;
}

class MockFinanceDataSource implements IFinanceDataSource {
  async fetchTransactions(params?: { type?: string; startDate?: string; endDate?: string }): Promise<Transaction[]> {
    return [
      { id: 'txn_001', type: 'income', amount: 1250.00, currency: 'USD', category: 'Sales', description: 'Order #ORD-001 payment', orderId: 'ORD-001', status: 'completed', createdAt: '2026-03-18 10:30:00' },
      { id: 'txn_002', type: 'expense', amount: 45.50, currency: 'USD', category: 'Shipping', description: 'FedEx shipping fee', status: 'completed', createdAt: '2026-03-18 11:00:00' },
      { id: 'txn_003', type: 'fee', amount: 187.50, currency: 'USD', category: 'Platform Fee', description: 'Amazon referral fee', orderId: 'ORD-001', status: 'completed', createdAt: '2026-03-18 10:35:00' },
      { id: 'txn_004', type: 'refund', amount: 89.99, currency: 'USD', category: 'Refund', description: 'Customer return refund', orderId: 'ORD-002', status: 'pending', createdAt: '2026-03-18 14:00:00' },
    ];
  }

  async createTransaction(data: Partial<Transaction>): Promise<Transaction> {
    return { ...data, id: `txn_${Date.now()}`, createdAt: new Date().toISOString() } as Transaction;
  }

  async fetchReconciliation(params?: { platform?: string; status?: string }): Promise<ReconciliationRecord[]> {
    return [
      { id: 'rec_001', platform: 'AMAZON', orderId: 'ORD-001', platformAmount: 1250.00, systemAmount: 1250.00, difference: 0, status: 'matched', reconciledAt: '2026-03-18 12:00:00', createdAt: '2026-03-18 10:30:00' },
      { id: 'rec_002', platform: 'AMAZON', orderId: 'ORD-002', platformAmount: 89.99, systemAmount: 85.99, difference: 4.00, status: 'mismatch', createdAt: '2026-03-18 11:00:00' },
      { id: 'rec_003', platform: 'EBAY', orderId: 'ORD-003', platformAmount: 350.00, systemAmount: 350.00, difference: 0, status: 'pending', createdAt: '2026-03-18 14:00:00' },
    ];
  }

  async reconcile(id: string, data: { action: 'approve' | 'reject'; note?: string }): Promise<ReconciliationRecord> {
    return { id, platform: 'AMAZON', orderId: 'ORD-001', platformAmount: 1250, systemAmount: 1250, difference: 0, status: 'matched', reconciledAt: new Date().toISOString(), createdAt: new Date().toISOString() };
  }

  async autoReconcile(platform: string): Promise<{ matched: number; mismatched: number }> {
    return { matched: 45, mismatched: 3 };
  }
}

class ApiFinanceDataSource implements IFinanceDataSource {
  private baseUrl = '/api/finance';

  async fetchTransactions(params?: { type?: string; startDate?: string; endDate?: string }): Promise<Transaction[]> {
    const query = new URLSearchParams(params as any).toString();
    const res = await fetch(`${this.baseUrl}/transactions?${query}`);
    return res.json();
  }

  async createTransaction(data: Partial<Transaction>): Promise<Transaction> {
    const res = await fetch(`${this.baseUrl}/transactions`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data) });
    return res.json();
  }

  async fetchReconciliation(params?: { platform?: string; status?: string }): Promise<ReconciliationRecord[]> {
    const query = new URLSearchParams(params as any).toString();
    const res = await fetch(`${this.baseUrl}/reconciliation?${query}`);
    return res.json();
  }

  async reconcile(id: string, data: { action: 'approve' | 'reject'; note?: string }): Promise<ReconciliationRecord> {
    const res = await fetch(`${this.baseUrl}/reconciliation/${id}`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data) });
    return res.json();
  }

  async autoReconcile(platform: string): Promise<{ matched: number; mismatched: number }> {
    const res = await fetch(`${this.baseUrl}/reconciliation/auto`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ platform }) });
    return res.json();
  }
}

const useMock = process.env.REACT_APP_USE_MOCK === 'true';
export const financeDataSource: IFinanceDataSource = useMock ? new MockFinanceDataSource() : new ApiFinanceDataSource();
