/**
 * 级联数据源预设配置
 */
import type { CascadeDataSourceConfig } from '../types';

/**
 * 环境 → 项目
 */
export const environmentProjectsConfig: CascadeDataSourceConfig = {
  description: '环境 → 项目',
  levels: [
    {
      url: '/api/v1/environment/list',
      labelField: 'name',
      valueField: 'id'
    },
    {
      url: '/api/v1/deployment-config/list',
      parentParam: 'environmentId',
      labelField: 'applicationName',
      valueField: 'id',
      isLeaf: () => true  // 第二级是叶子节点
    }
  ]
};

/**
 * Jenkins 服务器 → View → Job
 */
export const jenkinsServerViewsJobsConfig: CascadeDataSourceConfig = {
  description: 'Jenkins 服务器 → View → Job',
  levels: [
    {
      url: '/api/v1/external-system/list',
      params: { type: 'JENKINS' },
      labelField: 'name',
      valueField: 'id'
    },
    {
      url: '/api/v1/jenkins/views',
      parentParam: 'externalSystemId',
      labelField: 'viewName',
      valueField: 'id'
    },
    {
      url: '/api/v1/jenkins/jobs',
      parentParam: 'viewId',
      labelField: 'jobName',
      valueField: 'id',
      isLeaf: () => true
    }
  ]
};

/**
 * 部门 → 用户（固定两级）
 */
export const departmentUsersConfig: CascadeDataSourceConfig = {
  description: '部门 → 用户',
  levels: [
    {
      url: '/api/v1/department/list',
      labelField: 'name',
      valueField: 'code'
    },
    {
      url: '/api/v1/user/list',
      parentParam: 'departmentCode',
      labelField: 'nickname',
      valueField: 'username',
      isLeaf: () => true
    }
  ]
};

/**
 * 部门树（递归无限层级）⭐ 新增
 * 适用于组织架构等需要无限层级的场景
 */
export const departmentTreeConfig: CascadeDataSourceConfig = {
  description: '部门树（无限层级）',
  recursive: {
    url: '/api/v1/department/tree',
    parentParam: 'parentId',
    labelField: 'name',
    valueField: 'id',
    // 可选：自定义判断是否有子节点
    // hasChildren: (item) => item.hasChildren
    // 如果不提供，会自动使用后端返回的 isLeaf 或 hasChildren 字段
  }
};

/**
 * 项目组 → 应用
 */
export const projectGroupAppsConfig: CascadeDataSourceConfig = {
  description: '项目组 → 应用',
  levels: [
    {
      url: '/api/v1/project-group/list',
      labelField: 'name',
      valueField: 'id'
    },
    {
      url: '/api/v1/application/list',
      parentParam: 'projectGroupId',
      labelField: 'appName',
      valueField: 'id',
      isLeaf: () => true
    }
  ]
};

