import request from '@/utils/request'

// ========== 流水线 API ==========

/**
 * 分页查询流水线
 */
export function pagePipeline(params?: {
  page?: number
  limit?: number
  appId?: number
  ciType?: string
  enabled?: boolean
}) {
  return request.get<{
    code: number
    message: string
    data: {
      records: AppPipeline[]
      total: number
    }
  }>('/api/app/cicd/pipeline/page', { params })
}

/**
 * 查询应用的所有流水线
 */
export function listPipelineByApp(appId: number) {
  return request.get<{
    code: number
    message: string
    data: AppPipeline[]
  }>('/api/app/cicd/pipeline/app/' + appId)
}

/**
 * 查询流水线详情
 */
export function getPipeline(id: number) {
  return request.get<{
    code: number
    message: string
    data: AppPipeline
  }>('/api/app/cicd/pipeline/' + id)
}

/**
 * 创建流水线
 */
export function createPipeline(data: Partial<AppPipeline>) {
  return request.post<{
    code: number
    message: string
  }>('/api/app/cicd/pipeline', data)
}

/**
 * 更新流水线
 */
export function updatePipeline(data: Partial<AppPipeline>) {
  return request.put<{
    code: number
    message: string
  }>('/api/app/cicd/pipeline', data)
}

/**
 * 删除流水线
 */
export function deletePipeline(id: number) {
  return request.delete<{
    code: number
    message: string
  }>('/api/app/cicd/pipeline/' + id)
}

/**
 * 启用/禁用流水线
 */
export function togglePipeline(id: number, enabled: boolean) {
  return request.post<{
    code: number
    message: string
  }>('/api/app/cicd/pipeline/' + id + '/toggle?enabled=' + enabled)
}

/**
 * 获取流水线状态
 */
export function getPipelineStatus(id: number) {
  return request.get<{
    code: number
    message: string
    data: {
      id: number
      status: string
      lastBuildId?: number
      lastBuildTime?: string
      successCount: number
      failureCount: number
    }
  }>('/api/app/cicd/pipeline/' + id + '/status')
}

// ========== 构建 API ==========

/**
 * 分页查询构建记录
 */
export function pageBuild(params?: {
  page?: number
  limit?: number
  appId?: number
  status?: string
  ciType?: string
  branch?: string
}) {
  return request.get<{
    code: number
    message: string
    data: {
      records: AppBuild[]
      total: number
    }
  }>('/api/app/cicd/build/page', { params })
}

/**
 * 查询应用的所有构建记录
 */
export function listBuildByApp(appId: number) {
  return request.get<{
    code: number
    message: string
    data: AppBuild[]
  }>('/api/app/cicd/build/app/' + appId)
}

/**
 * 查询构建详情
 */
export function getBuild(id: number) {
  return request.get<{
    code: number
    message: string
    data: AppBuild
  }>('/api/app/cicd/build/' + id)
}

/**
 * 获取应用最新构建
 */
export function getLatestBuild(appId: number) {
  return request.get<{
    code: number
    message: string
    data: AppBuild | null
  }>('/api/app/cicd/build/latest/' + appId)
}

/**
 * 触发构建
 */
export function triggerBuild(appId: number, branch?: string) {
  return request.post<{
    code: number
    message: string
    data: {
      id: number
      buildNumber: string
      status: string
      branch: string
    }
  }>('/api/app/cicd/build/trigger', null, {
    params: { appId, branch }
  })
}

/**
 * 获取构建日志
 */
export function getBuildLog(id: number) {
  return request.get<{
    code: number
    message: string
    data: {
      log: string
      buildId: number
    }
  }>('/api/app/cicd/build/' + id + '/log')
}

/**
 * 取消构建
 */
export function cancelBuild(id: number) {
  return request.post<{
    code: number
    message: string
  }>('/api/app/cicd/build/' + id + '/cancel')
}

/**
 * 重试构建
 */
export function retryBuild(id: number) {
  return request.post<{
    code: number
    message: string
    data: {
      id: number
      buildNumber: string
      status: string
    }
  }>('/api/app/cicd/build/' + id + '/retry')
}

/**
 * 获取构建统计
 */
export function getBuildStats(appId: number) {
  return request.get<{
    code: number
    message: string
    data: {
      total: number
      success: number
      failed: number
      running: number
    }
  }>('/api/app/cicd/build/stats/' + appId)
}

/**
 * 获取CI系统配置
 */
export function getCIConfig() {
  return request.get<{
    code: number
    message: string
    data: {
      supportedCI: string[]
      defaultCI: string
      giteaUrl: string
    }
  }>('/api/app/cicd/config')
}

// ========== 类型定义 ==========

export interface AppPipeline {
  id?: number
  appId?: number
  name?: string
  description?: string
  ciType?: 'gitea' | 'jenkins' | 'github'
  repoFullName?: string
  workflowFile?: string
  stages?: string
  env?: 'development' | 'staging' | 'production'
  defaultBranch?: string
  enabled?: boolean
  autoDeploy?: boolean
  timeout?: number
  config?: string
  lastBuildId?: number
  lastBuildStatus?: string
  lastBuildTime?: string
  successCount?: number
  failureCount?: number
  userId?: number
  tenantId?: number
  createTime?: string
  updateTime?: string
}

export interface AppBuild {
  id?: number
  appId?: number
  versionId?: number
  buildNumber?: string
  name?: string
  branch?: string
  commitSha?: string
  commitMessage?: string
  commitAuthor?: string
  ciType?: 'gitea' | 'jenkins' | 'github'
  ciJobId?: string
  ciRunId?: string
  ciApiUrl?: string
  status?: 'pending' | 'running' | 'success' | 'failed' | 'cancelled'
  startedAt?: string
  finishedAt?: string
  duration?: number
  logUrl?: string
  artifactUrl?: string
  artifactName?: string
  artifactSize?: number
  errorMessage?: string
  triggerType?: 'manual' | 'webhook' | 'schedule'
  triggeredBy?: number
  config?: string
  userId?: number
  tenantId?: number
  createTime?: string
  updateTime?: string
}
