import type { BaseQuery, BaseResponse } from '@/types/base';
import type { Environment } from '@/pages/Deploy/Environment/List/types';
import type { Application } from '@/pages/Deploy/Application/List/types';

// 导出环境和应用类型供使用
export type { Environment, Application };

/**
 * 开发模式枚举
 */
export enum DevelopmentModeEnum {
  /** 标准模式 - 单仓库直接开发部署 */
  STANDARD = 'STANDARD',
  /** 同步模式 - 源码内外网同步 */
  SYNC_MODE = 'SYNC_MODE',
  /** 制品交付模式 - 仅交付编译制品，无源码 */
  ARTIFACT_DELIVERY = 'ARTIFACT_DELIVERY'
}

/**
 * 开发模式选项配置
 */
export interface DevelopmentModeOption {
  value: DevelopmentModeEnum;
  label: string;
  description: string;
  requiresExternalRepo: boolean;
  enablesGitSyncCheck: boolean;
}

/**
 * 开发模式选项列表
 */
export const DEVELOPMENT_MODE_OPTIONS: DevelopmentModeOption[] = [
  {
    value: DevelopmentModeEnum.STANDARD,
    label: '标准模式',
    description: '单仓库直接开发部署',
    requiresExternalRepo: false,
    enablesGitSyncCheck: false
  },
  {
    value: DevelopmentModeEnum.SYNC_MODE,
    label: '同步模式',
    description: '源码内外网同步',
    requiresExternalRepo: true,
    enablesGitSyncCheck: true
  },
  {
    value: DevelopmentModeEnum.ARTIFACT_DELIVERY,
    label: '制品交付模式',
    description: '仅交付编译制品，无源码',
    requiresExternalRepo: true,
    enablesGitSyncCheck: false
  }
];

/**
 * 团队查询参数
 */
export interface TeamQuery extends BaseQuery {
  teamCode?: string;
  teamName?: string;
  enabled?: boolean;
}

/**
 * 团队响应
 */
export interface TeamResponse extends BaseResponse {
  teamCode: string;
  teamName: string;
  description?: string;
  ownerId?: number;
  ownerName?: string;
  enabled: boolean;
  sort: number;
  developmentMode: DevelopmentModeEnum;      // 开发模式
  enableGitSyncCheck: boolean;                // 是否启用Git同步检测
  memberCount?: number;
  environmentCount?: number;
  applicationCount?: number;
}

/**
 * 团队创建/更新请求
 */
export interface TeamRequest {
  teamCode: string;
  teamName: string;
  description?: string;
  ownerId?: number;
  ownerName?: string;
  enabled?: boolean;
  sort?: number;
  developmentMode?: DevelopmentModeEnum;     // 开发模式
  enableGitSyncCheck?: boolean;               // 是否启用Git同步检测
}

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

/**
 * 通知配置
 */
export interface NotificationConfig {
  id?: number;
  teamId?: number;
  environmentId?: number;
  notificationChannelId?: number;
  preApprovalNotificationEnabled?: boolean;
  preApprovalNotificationTemplateId?: number;
  buildNotificationEnabled?: boolean;
  buildNotificationTemplateId?: number;
  buildFailureFileEnabled?: boolean;
  notificationChannelName?: string; // 关联数据
  channelType?: string; // 渠道类型：WEWORK, EMAIL, DINGTALK等
  updateTime?: string; // 更新时间
  updateBy?: string; // 更新人
}

/**
 * 团队环境配置响应
 */
export interface TeamEnvironmentConfig extends BaseResponse {
  teamId: number;
  environmentId: number;
  approvalRequired?: boolean;
  approverUserIds?: number[];
  requireCodeReview?: boolean;
  remark?: string;
  // 嵌套的通知配置
  notificationConfig?: NotificationConfig;
  // 关联数据
  environmentName?: string;
  applicationCount?: number;
}

/**
 * 团队环境配置请求
 */
export interface TeamEnvironmentConfigRequest {
  teamId: number;
  environmentId: number;
  approvalRequired?: boolean;
  approverUserIds?: number[];
  requireCodeReview?: boolean;
  remark?: string;
  // 嵌套的通知配置
  notificationConfig?: {
    id?: number;
    notificationChannelId?: number;
    preApprovalNotificationEnabled?: boolean;
    preApprovalNotificationTemplateId?: number;
    buildNotificationEnabled?: boolean;
    buildNotificationTemplateId?: number;
    buildFailureFileEnabled?: boolean;
  };
}

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

/**
 * 构建类型枚举
 */
export type BuildType = 'JENKINS' | 'NATIVE';

/**
 * 构建类型选项
 */
export const BUILD_TYPE_OPTIONS = [
  { value: 'JENKINS', label: 'Jenkins构建' },
  { value: 'NATIVE', label: '脚本部署' },
] as const;

/**
 * 团队应用关联响应
 */
export interface TeamApplication extends BaseResponse {
  teamId: number;
  applicationId: number;
  environmentId: number;
  buildType?: BuildType;  // 构建类型
  sourceBranch?: string;          // 源分支名称
  deploySystemId?: number;
  deployJob?: string;
  workflowDefinitionId?: number;
  sourceGitSystemId?: number;     // 源Git系统ID
  sourceGitProjectId?: number;    // 源Git项目ID
  targetGitSystemId?: number;     // 目标Git系统ID（仅SYNC_MODE）
  targetGitProjectId?: number;    // 目标Git项目ID
  targetBranch?: string;          // 目标分支名称
  teamName?: string;
  applicationName?: string;
  applicationCode?: string;
  environmentName?: string;
  deploySystemName?: string;
  workflowDefinitionName?: string;
  sourceGitSystemName?: string;   // 源Git系统名称
  sourceGitProjectName?: string;  // 源Git项目名称
  targetGitSystemName?: string;   // 目标Git系统名称
  targetGitProjectName?: string;  // 目标Git项目名称
}

/**
 * 团队应用关联请求
 */
export interface TeamApplicationRequest {
  teamId: number;
  applicationId: number;
  environmentId: number;
  buildType?: BuildType;  // 构建类型
  sourceBranch?: string;          // 源分支名称
  deploySystemId?: number;
  deployJob?: string;
  workflowDefinitionId?: number;
  sourceGitSystemId?: number;     // 源Git系统ID
  sourceGitProjectId?: number;    // 源Git项目ID
  targetGitSystemId?: number;     // 目标Git系统ID（仅SYNC_MODE）
  targetGitProjectId?: number;    // 目标Git项目ID
  targetBranch?: string;          // 目标分支名称
}

