/**
 * CI/CD 流水线 API
 */
import { request } from '../utils/request'
import type {
  Build,
  BuildParam,
  BuildStatus,
  Deploy,
  DeployParam,
  DeployStatus,
  PipelineConfig,
  RuntimeInstance,
} from '../types/cicd'

// ==================== 构建相关 ====================

/** 获取构建列表 */
export const pageBuild = (params: BuildParam) => {
  return request<{ list: Build[]; total: number }>({
    url: '/developer/build/page',
    method: 'GET',
    params,
  })
}

/** 获取构建详情 */
export const getBuild = (id: number) => {
  return request<Build>({
    url: `/developer/build/${id}`,
    method: 'GET',
  })
}

/** 触发构建 */
export const triggerBuild = (data: { websiteId: number; branch?: string; commitId?: string }) => {
  return request<Build>({
    url: '/developer/build/trigger',
    method: 'POST',
    data,
  })
}

/** 取消构建 */
export const cancelBuild = (id: number) => {
  return request<void>({
    url: `/developer/build/${id}/cancel`,
    method: 'POST',
  })
}

/** 获取构建日志 */
export const getBuildLogs = (id: number) => {
  return request<{ logs: string }>({
    url: `/developer/build/${id}/logs`,
    method: 'GET',
  })
}

/** 获取构建产物 */
export const getBuildArtifacts = (id: number) => {
  return request<{ artifacts: any[] }>({
    url: `/developer/build/${id}/artifacts`,
    method: 'GET',
  })
}

// ==================== 部署相关 ====================

/** 获取部署列表 */
export const pageDeploy = (params: DeployParam) => {
  return request<{ list: Deploy[]; total: number }>({
    url: '/developer/deploy/page',
    method: 'GET',
    params,
  })
}

/** 获取部署详情 */
export const getDeploy = (id: number) => {
  return request<Deploy>({
    url: `/developer/deploy/${id}`,
    method: 'GET',
  })
}

/** 触发部署 */
export const triggerDeploy = (data: {
  websiteId: number
  buildId: number
  env: string
}) => {
  return request<Deploy>({
    url: '/developer/deploy/trigger',
    method: 'POST',
    data,
  })
}

/** 回滚部署 */
export const rollbackDeploy = (id: number) => {
  return request<Deploy>({
    url: `/developer/deploy/${id}/rollback`,
    method: 'POST',
  })
}

/** 获取部署日志 */
export const getDeployLogs = (id: number) => {
  return request<{ logs: string }>({
    url: `/developer/deploy/${id}/logs`,
    method: 'GET',
  })
}

// ==================== 流水线配置相关 ====================

/** 获取流水线配置 */
export const getPipelineConfig = (websiteId: number) => {
  return request<PipelineConfig>({
    url: `/developer/pipeline/config/${websiteId}`,
    method: 'GET',
  })
}

/** 更新流水线配置 */
export const updatePipelineConfig = (data: PipelineConfig) => {
  return request<PipelineConfig>({
    url: '/developer/pipeline/config',
    method: 'PUT',
    data,
  })
}

/** 获取分支列表 */
export const getBranches = (websiteId: number) => {
  return request<{ branches: string[] }>({
    url: `/developer/pipeline/branches/${websiteId}`,
    method: 'GET',
  })
}

/** 添加环境变量 */
export const addEnvVar = (websiteId: number, data: { key: string; value: string; isSecret?: boolean }) => {
  return request<void>({
    url: `/developer/pipeline/env/${websiteId}`,
    method: 'POST',
    data,
  })
}

/** 删除环境变量 */
export const deleteEnvVar = (websiteId: number, key: string) => {
  return request<void>({
    url: `/developer/pipeline/env/${websiteId}/${key}`,
    method: 'DELETE',
  })
}

// ==================== 运行时相关 ====================

/** 获取运行时实例列表 */
export const pageRuntime = (websiteId: number, env?: string) => {
  return request<{ list: RuntimeInstance[]; total: number }>({
    url: '/developer/runtime/page',
    method: 'GET',
    params: { websiteId, env },
  })
}

/** 启动实例 */
export const startRuntime = (id: number) => {
  return request<void>({
    url: `/developer/runtime/${id}/start`,
    method: 'POST',
  })
}

/** 停止实例 */
export const stopRuntime = (id: number) => {
  return request<void>({
    url: `/developer/runtime/${id}/stop`,
    method: 'POST',
  })
}

/** 重启实例 */
export const restartRuntime = (id: number) => {
  return request<void>({
    url: `/developer/runtime/${id}/restart`,
    method: 'POST',
  })
}
