import { authClient } from '../auth/client';

export type TransactionKind = 'obtained' | 'consumed';

export interface Transaction {
  id: string;
  title: string;
  happenedAt: string;
  amount: number;
  kind: TransactionKind;
}

export interface TransactionsResponse {
  success: boolean;
  data: Transaction[];
  message?: string;
}

/**
 * 获取用户积分交易记录
 * 通过 subscription.meterEvent 获取消费记录,通过订阅历史获取充值记录
 */
export async function getUserTransactions(): Promise<TransactionsResponse> {
  try {
    const { data: subscriptions, error } = await authClient.subscription.list({});

    if (error) {
      return {
        success: false,
        data: [],
        message: error.message || '获取交易记录失败',
      };
    }

    const transactions: Transaction[] = [];

    // 从订阅中提取 meter events (消费记录)
    subscriptions?.forEach((sub: any) => {
      if (sub.type === 'metered' && sub.meterEvents) {
        sub.meterEvents.forEach((event: any) => {
          const value = parseInt(event.payload?.value || '0');
          if (value > 0) {
            transactions.push({
              id: event.id || `event-${Date.now()}-${Math.random()}`,
              title: event.event_name === 'token_usage' ? 'Token消费' : '积分消费',
              happenedAt: event.createdAt || new Date().toISOString(),
              amount: -value,
              kind: 'consumed',
            });
          }
        });
      }

      // 提取充值记录 (从订阅创建或续费事件)
      if (sub.createdAt) {
        const grantToken = parseInt(sub.plan?.metadata?.grant_token || '0');
        if (grantToken > 0) {
          transactions.push({
            id: `sub-${sub.id}`,
            title: `订阅充值 - ${sub.plan?.name || '未知套餐'}`,
            happenedAt: sub.createdAt,
            amount: grantToken,
            kind: 'obtained',
          });
        }
      }
    });

    // 按时间倒序排序
    transactions.sort((a, b) =>
      new Date(b.happenedAt).getTime() - new Date(a.happenedAt).getTime()
    );

    return {
      success: true,
      data: transactions,
    };
  } catch (error) {
    console.error('Failed to get user transactions:', error);
    return {
      success: false,
      data: [],
      message: '网络错误，请稍后重试',
    };
  }
}
