// 开发者权限申请 & Git 账号（对齐 PC /developer/requests、/git 与 /admin/git-review，基址走 websopy-api）
import { get, post, put } from '@/utils/request'
import { WebsopyBaseUrl } from '@/config/app'

const BASE = '/app/developer'

export interface PermissionRequest {
  id?: number
  userId?: number
  gitUsername?: string
  repo?: string
  repoName?: string
  reason?: string
  status?: string // pending / approved / rejected
  createTime?: string
  reviewedAt?: string
  reviewerName?: string
  rejectReason?: string
  [key: string]: any
}

export interface GitAccount {
  id?: number
  userId?: number
  username?: string
  email?: string
  status?: string // pending / verified / rejected
  verificationNote?: string
  tenantId?: number
  [key: string]: any
}

// ============ 权限申请 ============
export async function pagePermissionRequests(params: any) {
  const res = await get<any>(`${BASE}/permission-requests`, params, { baseUrl: WebsopyBaseUrl })
  if (res?.code === 0) return res.data
  return Promise.reject(new Error(res?.message || '加载失败'))
}

export async function getPermissionRequestStats() {
  const res = await get<any>(`${BASE}/permission-requests/stats`, undefined, { baseUrl: WebsopyBaseUrl })
  if (res?.code === 0) return res.data
  return Promise.reject(new Error(res?.message || '加载失败'))
}

export async function getAvailableRepos() {
  const res = await get<any>(`${BASE}/available-repos`, undefined, { baseUrl: WebsopyBaseUrl })
  if (res?.code === 0) return res.data
  return Promise.reject(new Error(res?.message || '加载失败'))
}

export async function submitPermissionRequest(data: any) {
  const res = await post<any>(`${BASE}/permission-requests`, data, { baseUrl: WebsopyBaseUrl })
  if (res?.code === 0) return res.data
  return Promise.reject(new Error(res?.message || '提交失败'))
}

export async function approvePermissionRequest(id: number) {
  const res = await put<any>(`${BASE}/permission-requests/${id}/approve`, {}, { baseUrl: WebsopyBaseUrl })
  if (res?.code === 0) return res.message
  return Promise.reject(new Error(res?.message || '操作失败'))
}

export async function rejectPermissionRequest(id: number, reason: string) {
  const res = await put<any>(`${BASE}/permission-requests/${id}/reject`, { reason }, { baseUrl: WebsopyBaseUrl })
  if (res?.code === 0) return res.message
  return Promise.reject(new Error(res?.message || '操作失败'))
}

// ============ Git 账号 ============
export async function saveGitAccount(data: any) {
  const res = await post<any>(`${BASE}/git-account`, data, { baseUrl: WebsopyBaseUrl })
  if (res?.code === 0) return res.data
  return Promise.reject(new Error(res?.message || '提交失败'))
}

export async function getGitAccountStatus() {
  const res = await get<any>(`${BASE}/git-account/status`, undefined, { baseUrl: WebsopyBaseUrl })
  if (res?.code === 0) return res.data
  return Promise.reject(new Error(res?.message || '加载失败'))
}

export async function getGiteaInfo() {
  const res = await get<any>(`${BASE}/gitea-info`, undefined, { baseUrl: WebsopyBaseUrl })
  if (res?.code === 0) return res.data
  return Promise.reject(new Error(res?.message || '加载失败'))
}

// ============ 管理端 ============
export async function pageGitAccounts(params: any) {
  const res = await get<any>(`${BASE}/git-accounts`, params, { baseUrl: WebsopyBaseUrl })
  if (res?.code === 0) return res.data
  return Promise.reject(new Error(res?.message || '加载失败'))
}

export async function approveGitAccount(id: number) {
  const res = await put<any>(`${BASE}/git-accounts/${id}/approve`, {}, { baseUrl: WebsopyBaseUrl })
  if (res?.code === 0) return res.message
  return Promise.reject(new Error(res?.message || '操作失败'))
}

export async function rejectGitAccount(id: number, reason: string) {
  const res = await put<any>(`${BASE}/git-accounts/${id}/reject`, { reason }, { baseUrl: WebsopyBaseUrl })
  if (res?.code === 0) return res.message
  return Promise.reject(new Error(res?.message || '操作失败'))
}
