import { http } from './http';
import { BaseDataSource, DataSourceFactory } from './baseDataSource';
/**
 * [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 extends BaseDataSource implements IFinanceDataSource {
  async fetchTransactions(params?: { type?: string; startDate?: string; endDate?: string }): Promise<Transaction[]> {
    try {
      await this.delay(300);
      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' },
      ];
    } catch (error) {
      return this.handleError(error, []);
    }
  }

  async createTransaction(data: Partial<Transaction>): Promise<Transaction> {
    try {
      await this.delay(200);
      return { ...data, id: `txn_${Date.now()}`, createdAt: new Date().toISOString() } as Transaction;
    } catch (error) {
      throw error;
    }
  }

  async fetchReconciliation(params?: { platform?: string; status?: string }): Promise<ReconciliationRecord[]> {
    try {
      await this.delay(300);
      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' },
      ];
    } catch (error) {
      return this.handleError(error, []);
    }
  }

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

  async autoReconcile(platform: string): Promise<{ matched: number; mismatched: number }> {
    try {
      await this.delay(500);
      return { matched: 45, mismatched: 3 };
    } catch (error) {
      return this.handleError(error, { matched: 0, mismatched: 0 });
    }
  }
}

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

  async fetchTransactions(params?: { type?: string; startDate?: string; endDate?: string }): Promise<Transaction[]> {
    try {
      const query = new URLSearchParams(params as any).toString();
      const res = await http.get(`${this.baseUrl}/transactions?${query}`);
      return res.data;
    } catch (error) {
      return this.handleError(error, []);
    }
  }

  async createTransaction(data: Partial<Transaction>): Promise<Transaction> {
    try {
      const res = await http.post(`${this.baseUrl}/transactions`, data);
      return res.data;
    } catch (error) {
      throw error;
    }
  }

  async fetchReconciliation(params?: { platform?: string; status?: string }): Promise<ReconciliationRecord[]> {
    try {
      const query = new URLSearchParams(params as any).toString();
      const res = await http.get(`${this.baseUrl}/reconciliation?${query}`);
      return res.data;
    } catch (error) {
      return this.handleError(error, []);
    }
  }

  async reconcile(id: string, data: { action: 'approve' | 'reject'; note?: string }): Promise<ReconciliationRecord> {
    try {
      const res = await http.post(`${this.baseUrl}/reconciliation/${id}`, data);
      return res.data;
    } catch (error) {
      throw error;
    }
  }

  async autoReconcile(platform: string): Promise<{ matched: number; mismatched: number }> {
    try {
      const res = await http.post(`${this.baseUrl}/reconciliation/auto`, { platform });
      return res.data;
    } catch (error) {
      return this.handleError(error, { matched: 0, mismatched: 0 });
    }
  }
}

const useMock = process.env.NODE_ENV === 'development' || process.env.REACT_APP_USE_MOCK === 'true';
const financeDataSourceInstance: IFinanceDataSource = useMock ? new MockFinanceDataSource() : new ApiFinanceDataSource();

// 注册到数据源工厂
DataSourceFactory.register('finance', financeDataSourceInstance);

export const financeDataSource = financeDataSourceInstance;
