/**
 * 认种树定价配置服务
 * 总部运营成本压力涨价管理：手动调价 + 自动涨价 + 审计日志
 * [2026-02-26] 新增
 */

import apiClient from '@/infrastructure/api/client';
import { API_ENDPOINTS } from '@/infrastructure/api/endpoints';

/** 定价配置响应 */
export interface TreePricingConfig {
  basePrice: number;
  basePortionPrice: number;
  currentSupplement: number;
  totalPrice: number;
  totalPortionPrice: number;
  autoIncreaseEnabled: boolean;
  autoIncreaseAmount: number;
  autoIncreaseIntervalDays: number;
  lastAutoIncreaseAt: string | null;
  nextAutoIncreaseAt: string | null;
  updatedAt: string;
}

/** 变更日志项 */
export interface TreePriceChangeLogItem {
  id: string;
  changeType: 'MANUAL' | 'AUTO';
  previousSupplement: number;
  newSupplement: number;
  changeAmount: number;
  reason: string | null;
  operatorId: string | null;
  createdAt: string;
}

/** 变更日志分页响应 */
export interface TreePriceChangeLogResponse {
  items: TreePriceChangeLogItem[];
  total: number;
}

/**
 * 认种树定价配置服务
 */
export const treePricingService = {
  /** 获取当前定价配置 */
  async getConfig(): Promise<TreePricingConfig> {
    return apiClient.get(API_ENDPOINTS.TREE_PRICING.CONFIG);
  },

  /** 手动修改加价金额（总部运营成本压力涨价） */
  async updateSupplement(newSupplement: number, reason: string): Promise<TreePricingConfig> {
    return apiClient.post(API_ENDPOINTS.TREE_PRICING.SUPPLEMENT, { newSupplement, reason });
  },

  /** 更新自动涨价设置 */
  async updateAutoIncrease(params: {
    enabled: boolean;
    amount?: number;
    intervalDays?: number;
  }): Promise<TreePricingConfig> {
    return apiClient.put(API_ENDPOINTS.TREE_PRICING.AUTO_INCREASE, params);
  },

  /** 获取变更审计日志 */
  async getChangeLog(page: number = 1, pageSize: number = 20): Promise<TreePriceChangeLogResponse> {
    return apiClient.get(API_ENDPOINTS.TREE_PRICING.CHANGE_LOG, {
      params: { page, pageSize },
    });
  },
};
