import { Injectable } from '@nestjs/common'
import { InjectModel } from '@nestjs/mongoose'
import { Model } from 'mongoose'
import { SiteConfig, SiteConfigDocument } from './site-config.schema'

interface PricingConfig {
  interview: { pricePerSession: number; creditsPerPurchase: number }
  resumeOptimize: { freeLimit: number; pricePerOptimize: number; creditsPerPurchase: number }
  resumeDownload: { pricePerDownload: number; creditsPerPurchase: number }
  plans: {
    growth: { price: number; durationDays: number; credits: { interview: number; resumeOptimize: number; resumeDownload: number }; features: string[] }
    sprint: { price: number; durationDays: number; credits: { interview: number; resumeOptimize: number; resumeDownload: number }; features: string[] }
  }
}

const DEFAULT_PRICING: PricingConfig = {
  interview: { pricePerSession: 500, creditsPerPurchase: 1 },
  resumeOptimize: { freeLimit: 3, pricePerOptimize: 300, creditsPerPurchase: 1 },
  resumeDownload: { pricePerDownload: 200, creditsPerPurchase: 1 },
  plans: {
    growth: { price: 1990, durationDays: 30, credits: { interview: 999, resumeOptimize: 20, resumeDownload: 10 }, features: ['免费版全部权益', 'AI 数字人面试无限次', '详细面试报告（四维评分）', '进步轨迹雷达图 + 打卡', '每日一题推送', '参考回答思路', '公司真题库', '简历优化 20 次/月', '简历下载 10 次/月'] },
    sprint: { price: 4990, durationDays: 30, credits: { interview: 999, resumeOptimize: 50, resumeDownload: 30 }, features: ['成长版全部权益', 'AI 语音分析（语气词/语速检测）', '技能缺口分析报告', '学习路径推荐', '真人导师 1v1 点评（每月 1 次）', '简历精修（每月 1 次）', '内推优先', '简历优化 50 次/月', '简历下载 30 次/月'] },
  },
}

@Injectable()
export class PricingService {
  private cache: PricingConfig | null = null
  private cacheTime = 0

  constructor(
    @InjectModel(SiteConfig.name) private configModel: Model<SiteConfigDocument>,
  ) {}

  async getConfig(): Promise<PricingConfig> {
    // Cache for 60s
    if (this.cache && Date.now() - this.cacheTime < 60000) {
      return this.cache
    }
    try {
      const doc = await this.configModel.findOne({ key: 'pricing' }).exec()
      if (doc?.value) {
        this.cache = this.mergeDefaults(doc.value)
        this.cacheTime = Date.now()
        return this.cache
      }
    } catch {}
    return DEFAULT_PRICING
  }

  invalidateCache() {
    this.cache = null
  }

  private mergeDefaults(value: any): PricingConfig {
    return {
      interview: { ...DEFAULT_PRICING.interview, ...value?.interview },
      resumeOptimize: { ...DEFAULT_PRICING.resumeOptimize, ...value?.resumeOptimize },
      resumeDownload: { ...DEFAULT_PRICING.resumeDownload, ...value?.resumeDownload },
      plans: {
        growth: { ...DEFAULT_PRICING.plans.growth, ...value?.plans?.growth, credits: { ...DEFAULT_PRICING.plans.growth.credits, ...value?.plans?.growth?.credits } },
        sprint: { ...DEFAULT_PRICING.plans.sprint, ...value?.plans?.sprint, credits: { ...DEFAULT_PRICING.plans.sprint.credits, ...value?.plans?.sprint?.credits } },
      },
    }
  }
}
