import type { Recordable } from '@vben/types';

import { requestClient } from '#/api/request';

export namespace FeatureApi {
  export interface FeatureItem {
    id: number;
    api_id: string;
    name: string;
    whitelist_price: number;
    cost_price: number;
    create_time: string;
    update_time: string;
  }

  export interface FeatureList {
    total: number;
    items: FeatureItem[];
  }

  export interface CreateFeatureRequest {
    api_id: string;
    name: string;
    whitelist_price?: number;
    cost_price?: number;
  }

  export interface UpdateFeatureRequest {
    api_id?: string;
    name?: string;
    whitelist_price?: number;
    cost_price?: number;
  }

  export interface FeatureExampleItem {
    id: number;
    feature_id: number;
    api_id: string;
    data: string;
    create_time: string;
    update_time: string;
  }

  export interface ConfigFeatureExampleRequest {
    feature_id: number;
    data: string;
  }

  export interface ConfigFeatureExampleResponse {
    success: boolean;
  }

  export interface GetFeatureExampleRequest {
    feature_id: number;
  }

  export interface GetFeatureExampleResponse {
    id: number;
    feature_id: number;
    api_id: string;
    data: string;
    create_time: string;
    update_time: string;
  }
}

/**
 * 获取模块列表数据
 */
async function getFeatureList(params: Recordable<any>) {
  return requestClient.get<FeatureApi.FeatureList>('/feature/list', {
    params,
  });
}

/**
 * 获取模块详情
 * @param id 模块ID
 */
async function getFeatureDetail(id: number) {
  return requestClient.get<FeatureApi.FeatureItem>(`/feature/detail/${id}`);
}

/**
 * 创建模块
 * @param data 模块数据
 */
async function createFeature(data: FeatureApi.CreateFeatureRequest) {
  return requestClient.post<{ id: number }>('/feature/create', data);
}

/**
 * 更新模块
 * @param id 模块ID
 * @param data 模块数据
 */
async function updateFeature(
  id: number,
  data: FeatureApi.UpdateFeatureRequest,
) {
  return requestClient.put<{ success: boolean }>(`/feature/update/${id}`, data);
}

/**
 * 删除模块
 * @param id 模块ID
 */
async function deleteFeature(id: number) {
  return requestClient.delete<{ success: boolean }>(`/feature/delete/${id}`);
}

/**
 * 配置功能示例数据
 * @param data 示例数据配置
 */
async function configFeatureExample(
  data: FeatureApi.ConfigFeatureExampleRequest,
) {
  return requestClient.post<FeatureApi.ConfigFeatureExampleResponse>(
    '/feature/config-example',
    data,
  );
}

/**
 * 获取功能示例数据
 * @param featureId 功能ID
 */
async function getFeatureExample(featureId: number) {
  return requestClient.get<FeatureApi.GetFeatureExampleResponse>(
    `/feature/example/${featureId}`,
  );
}

export {
  configFeatureExample,
  createFeature,
  deleteFeature,
  getFeatureDetail,
  getFeatureExample,
  getFeatureList,
  updateFeature,
};
