import { Injectable, Logger } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import axios from 'axios';

export interface UserStats {
  totalUsers: number;
  activeUsers: number;
  newUsersToday: number;
}

export interface UserStatsWithChange {
  totalUsers: number;
  totalUsersChange: number;
  activeUsers: number;
  activeUsersChange: number;
}

@Injectable()
export class IdentityServiceClient {
  private readonly logger = new Logger(IdentityServiceClient.name);
  private readonly baseUrl: string;

  constructor(private readonly configService: ConfigService) {
    this.baseUrl = this.configService.get<string>(
      'IDENTITY_SERVICE_URL',
      'http://localhost:3001',
    );
  }

  /**
   * 获取用户统计数据
   */
  async getUserStats(): Promise<UserStats> {
    this.logger.debug(`Fetching user stats from ${this.baseUrl}`);

    try {
      // TODO: 实现真实API调用
      // const response = await axios.get(`${this.baseUrl}/internal/stats/users`);
      // return response.data;

      // Mock data for development
      return {
        totalUsers: 15680,
        activeUsers: 3240,
        newUsersToday: 45,
      };
    } catch (error) {
      this.logger.error('Failed to fetch user stats', error);
      return {
        totalUsers: 0,
        activeUsers: 0,
        newUsersToday: 0,
      };
    }
  }

  /**
   * 获取用户统计数据（含环比变化）
   */
  async getUserStatsWithChange(): Promise<UserStatsWithChange> {
    this.logger.debug(`Fetching user stats with change from ${this.baseUrl}`);

    try {
      // TODO: 实现真实API调用
      // const response = await axios.get(`${this.baseUrl}/internal/stats/users/change`);
      // return response.data;

      // Mock data for development
      return {
        totalUsers: 15680,
        totalUsersChange: 8.5,
        activeUsers: 3240,
        activeUsersChange: 3.2,
      };
    } catch (error) {
      this.logger.error('Failed to fetch user stats with change', error);
      return {
        totalUsers: 0,
        totalUsersChange: 0,
        activeUsers: 0,
        activeUsersChange: 0,
      };
    }
  }
}
