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

export interface PrePlantingEligibility {
  hasPrePlanting: boolean;
  totalPortions: number;
  totalTreesMerged: number;
  canApplyAuthorization: boolean;
  canTrade: boolean;
}

/**
 * 调用 planting-service 内部 API 查询预种资格
 */
@Injectable()
export class PrePlantingClient {
  private readonly logger = new Logger(PrePlantingClient.name);
  private readonly baseUrl: string;

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

  async getEligibility(accountSequence: string): Promise<PrePlantingEligibility> {
    const url = `${this.baseUrl}/internal/pre-planting/eligibility/${accountSequence}`;
    const response = await axios.get<PrePlantingEligibility>(url, {
      timeout: 3000,
    });
    return response.data;
  }
}
