import request from '@/utils/request';
import type { Page } from '@/types/base';
import type {
  TeamQuery,
  TeamResponse,
  TeamRequest,
  TeamEnvironmentConfig,
  TeamEnvironmentConfigRequest,
  TeamApplication,
  TeamApplicationRequest,
} from './types';

const BASE_URL = '/api/v1/teams';
const TEAM_CONFIG_URL = '/api/v1/team-configs';
const TEAM_APPLICATION_URL = '/api/v1/team-applications';

/**
 * 分页查询团队
 */
export const getTeams = (params?: TeamQuery) =>
  request.get<Page<TeamResponse>>(`${BASE_URL}/page`, { params });

/**
 * 查询团队详情
 */
export const getTeamById = (id: number) =>
  request.get<TeamResponse>(`${BASE_URL}/${id}`);

/**
 * 查询所有启用的团队（常用）
 */
export const getEnabledTeams = () =>
  request.get<TeamResponse[]>(`${BASE_URL}/list`);

/**
 * 创建团队
 */
export const createTeam = (data: TeamRequest) =>
  request.post<TeamResponse>(BASE_URL, data);

/**
 * 更新团队
 */
export const updateTeam = (id: number, data: TeamRequest) =>
  request.put<TeamResponse>(`${BASE_URL}/${id}`, data);

/**
 * 删除团队
 */
export const deleteTeam = (id: number) =>
  request.delete(`${BASE_URL}/${id}`);

// ==================== 环境和应用（复用已有接口） ====================

/**
 * 获取环境列表
 */
export { getEnvironmentList } from '@/pages/Deploy/Environment/List/service';

/**
 * 获取应用列表
 */
export { getApplicationListByCondition as getApplicationList } from '@/pages/Deploy/Application/List/service';

// ==================== 团队配置相关 ====================

// 旧的团队配置 API（已废弃，使用 TeamEnvironmentConfig 代替）
// export const getTeamConfig = (teamId: number) => ...
// export const createTeamConfig = (data: TeamConfigRequest) => ...
// export const updateTeamConfig = (id: number, data: TeamConfigRequest) => ...

// ==================== 团队应用关联相关 ====================

/**
 * 获取团队应用关联列表（支持分页和列表模式）
 */
export const getTeamApplications = (teamId: number, environmentId?: number) =>
  request.get<TeamApplication[]>(`${TEAM_APPLICATION_URL}/list`, {
    params: { teamId, environmentId }
  });

/**
 * 获取团队应用关联列表（分页）
 */
export const getTeamApplicationsPage = (params?: { teamId?: number; environmentId?: number; pageNum?: number; size?: number }) =>
  request.get<Page<TeamApplication>>(`${TEAM_APPLICATION_URL}/page`, { params });

/**
 * 创建团队应用关联
 */
export const createTeamApplication = (data: TeamApplicationRequest) =>
  request.post<TeamApplication>(TEAM_APPLICATION_URL, data);

/**
 * 更新团队应用关联
 */
export const updateTeamApplication = (id: number, data: TeamApplicationRequest) =>
  request.put<TeamApplication>(`${TEAM_APPLICATION_URL}/${id}`, data);

/**
 * 删除团队应用关联
 */
export const deleteTeamApplication = (id: number) =>
  request.delete(`${TEAM_APPLICATION_URL}/${id}`);

// ==================== Jenkins 相关 ====================

/**
 * 获取 Jenkins 系统列表
 */
export const getJenkinsSystems = () =>
  request.get<any[]>('/api/v1/external-system/list', {
    params: { type: 'JENKINS' }
  });

/**
 * 获取 Jenkins Job 列表
 */
export const getJenkinsJobs = (externalSystemId: number) =>
  request.get<any[]>('/api/v1/jenkins-job/list', {
    params: { externalSystemId }
  });

// ==================== 工作流相关 ====================

/**
 * 获取已发布的工作流定义列表
 */
export const getWorkflowDefinitions = () =>
  request.get<any[]>('/api/v1/workflow/definition/published');

// ==================== 通知渠道相关 ====================

/**
 * 获取通知渠道列表
 */
export const getNotificationChannels = () =>
  request.get<any[]>('/api/v1/notification-channel/list');

// ==================== 团队环境配置相关 ====================

/**
 * 查询团队的所有环境配置列表
 */
export const getTeamEnvironmentConfigs = (teamId: number) =>
  request.get<any[]>(`/api/v1/team-environment-config/team/${teamId}`);

/**
 * 根据团队ID和环境ID查询配置
 */
export const getTeamEnvironmentConfig = (teamId: number, environmentId: number) =>
  request.get<any>(
    `/api/v1/team-environment-config/team/${teamId}/environment/${environmentId}`
  );

/**
 * 创建团队环境配置
 */
export const createTeamEnvironmentConfig = (data: TeamEnvironmentConfigRequest) =>
  request.post('/api/v1/team-environment-config', data);

/**
 * 更新团队环境配置
 */
export const updateTeamEnvironmentConfig = (
  id: number,
  data: TeamEnvironmentConfigRequest
) =>
  request.put(`/api/v1/team-environment-config/${id}`, data);

/**
 * 删除团队环境配置
 */
export const deleteTeamEnvironmentConfig = (id: number) =>
  request.delete(`/api/v1/team-environment-config/${id}`);

/**
 * 获取通知模板列表
 */
export const getNotificationTemplates = (channelType?: string) =>
  request.get<any[]>('/api/v1/notification-template/list', {
    params: channelType ? { channelType } : undefined
  });


