import request from '@/utils/request'

// ========== 权限申请接口 ==========

/**
 * 获取权限申请列表
 */
export function getPermissionRequests(params?: {
  page?: number
  size?: number
  status?: 'pending' | 'approved' | 'rejected'
}) {
  return request.get<{
    code: number
    message: string
    data: {
      records: Array<{
        id: string | number
        repo: string
        repoName: string
        reason: string
        status: 'pending' | 'approved' | 'rejected'
        gitUsername: string
        createdAt: string
        reviewedAt?: string
        reviewerName?: string
        rejectReason?: string
      }>
      total: number
    }
  }>('/api/app/developer/permission-requests', { params })
}

/**
 * 提交权限申请
 */
export function createPermissionRequest(data: {
  repo: string
  reason: string
  gitUsername?: string
}) {
  return request.post<{
    code: number
    message: string
    data: {
      id: string | number
      repo: string
      repoName: string
      reason: string
      status: 'pending'
      createdAt: string
    }
  }>('/api/app/developer/permission-requests', data)
}

/**
 * 获取权限申请统计
 */
export function getPermissionRequestStats() {
  return request.get<{
    code: number
    message: string
    data: {
      pending: number
      approved: number
      rejected: number
      total: number
    }
  }>('/api/app/developer/permission-requests/stats')
}

/**
 * 获取可申请的仓库列表
 */
export function getAvailableRepositories() {
  return request.get<{
    code: number
    message: string
    data: Array<{
      value: string
      label: string
      description: string
      accessLevel?: 'read' | 'write' | 'admin'
      isAccessible: boolean
    }>
  }>('/api/app/developer/permission-requests/available-repos')
}

// ========== Git账号绑定接口 ==========

/**
 * 保存Git账号绑定信息
 */
export function saveGitAccount(data: {
  username: string
  email?: string
  remark?: string
}) {
  return request.post<{
    code: number
    message: string
    data: {
      userId: number
      gitUsername: string
      email?: string
      remark?: string
      savedAt: string
      status: 'pending' | 'verified' | 'rejected'
    }
  }>('/api/app/developer/git-account', data)
}

/**
 * 获取Git账号绑定状态
 */
export function getGitAccountStatus() {
  return request.get<{
    code: number
    message: string
    data: {
      username?: string
      email?: string
      remark?: string
      status: 'pending' | 'verified' | 'rejected' | 'not_bound'
      lastUpdatedAt?: string
      verificationNote?: string
    }
  }>('/api/app/developer/git-account/status')
}

/**
 * 获取Gitea服务器信息
 */
export function getGiteaServerInfo() {
  return request.get<{
    code: number
    message: string
    data: {
      url: string
      version: string
      registrationEnabled: boolean
      requireEmailConfirmation: boolean
      maxRepoCreation: number
    }
  }>('/api/app/developer/gitea-info')
}

// ========== 管理端 - Git账号审核接口 ==========

export interface GitAccountItem {
  id: number
  userId: number
  username: string
  email?: string
  remark?: string
  status: 'pending' | 'verified' | 'rejected'
  verificationNote?: string
  createTime: string
  updateTime: string
  tenantId: number
}

/**
 * 分页查询Git账号绑定列表（管理端）
 */
export function pageGitAccounts(params?: {
  page?: number
  size?: number
  status?: string
  keyword?: string
}) {
  return request.get<{
    code: number
    message: string
    data: {
      records: GitAccountItem[]
      total: number
      current: number
      size: number
    }
  }>('/api/app/developer/git-account/list', { params })
}

/**
 * 审核Git账号绑定 - 通过
 */
export function approveGitAccount(id: number, note?: string) {
  return request.put<{
    code: number
    message: string
  }>(`/api/app/developer/git-account/${id}/approve`, { note })
}

/**
 * 审核Git账号绑定 - 拒绝
 */
export function rejectGitAccount(id: number, reason: string) {
  return request.put<{
    code: number
    message: string
  }>(`/api/app/developer/git-account/${id}/reject`, { reason })
}

// ========== 管理端 - 权限申请审核接口 ==========

export interface PermissionRequestItem {
  id: number
  userId: number
  gitUsername: string
  repo: string
  repoName: string
  reason: string
  status: 'pending' | 'approved' | 'rejected'
  createdAt: string
  reviewedAt?: string
  reviewerName?: string
  rejectReason?: string
}

/**
 * 分页查询权限申请列表（管理端）
 */
export function pagePermissionRequestsAdmin(params?: {
  page?: number
  size?: number
  status?: string
  keyword?: string
}) {
  return request.get<{
    code: number
    message: string
    data: {
      records: PermissionRequestItem[]
      total: number
      current: number
      size: number
    }
  }>('/api/app/developer/permission-requests/page', { params })
}

/**
 * 审核权限申请 - 通过
 */
export function approvePermissionRequest(id: number, note?: string) {
  return request.put<{
    code: number
    message: string
  }>(`/api/app/developer/permission-requests/${id}/approve`, { note })
}

/**
 * 审核权限申请 - 拒绝
 */
export function rejectPermissionRequest(id: number, reason: string) {
  return request.put<{
    code: number
    message: string
  }>(`/api/app/developer/permission-requests/${id}/reject`, { reason })
}