// api/tool/gen.ts
import type { AxiosResponse } from 'axios'
import type { PageInfo, Result } from '@cacp/ui'
import request from '@/utils/request'

// 类型定义
export interface GenTableInfo {
  tableId: number
  tableName: string
  tableComment: string
  className: string
  createTime: string
  updateTime: string
  genType?: string
  genPath?: string
}

export interface DbTableInfo {
  tableName: string
  tableComment: string
  createTime: string
  updateTime: string
}

export interface GenTableQuery {
  pageNum?: number
  pageSize?: number
  tableName?: string
  tableComment?: string
  beginTime?: string
  endTime?: string
}

export interface ImportTableParams {
  tables: string
}

export interface CreateTableParams {
  tableName: string
  tableComment: string
  // 其他创建表需要的参数
}

export interface PreviewResult {
  [key: string]: string
}

const contextPath = '/tool/gen'

// 查询生成表数据
export async function listTable(query: GenTableQuery): Promise<Result<PageInfo<GenTableInfo>>> {
  const res: AxiosResponse<Result<PageInfo<GenTableInfo>>> = await request.get(
    `${contextPath}/list`,
    { params: query }
  )
  return res.data
}

// 查询db数据库列表
export async function listDbTable(query: GenTableQuery): Promise<Result<PageInfo<DbTableInfo>>> {
  const res: AxiosResponse<Result<PageInfo<DbTableInfo>>> = await request.get(
    `${contextPath}/db/list`,
    { params: query }
  )
  return res.data
}

// 查询表详细信息
export async function getGenTable(tableId: number): Promise<Result<GenTableInfo>> {
  const res: AxiosResponse<Result<GenTableInfo>> = await request.get(
    `${contextPath}/${tableId}`
  )
  return res.data
}

// 修改代码生成信息
export async function updateGenTable(data: GenTableInfo): Promise<Result<number>> {
  console.log(data)
  const res: AxiosResponse<Result<number>> = await request.put(
    `${contextPath}`,
    data
  )
  return res.data
}

// 导入表
export async function importTable(data: ImportTableParams): Promise<Result<number>> {
  const res: AxiosResponse<Result<number>> = await request.post(
    `${contextPath}/importTable`,
    null,
    { params: data }
  )
  return res.data
}

// 创建表
export async function createTable(data: CreateTableParams): Promise<Result<number>> {
  const res: AxiosResponse<Result<number>> = await request.post(
    `${contextPath}/createTable`,
    null,
    { params: data }
  )
  return res.data
}

// 预览生成代码
export async function previewTable(tableId: number): Promise<Result<PreviewResult>> {
  const res: AxiosResponse<Result<PreviewResult>> = await request.get(
    `${contextPath}/preview/${tableId}`
  )
  return res.data
}

// 删除表数据
export async function delTable(tableId: number | number[]): Promise<Result<number>> {
  const ids = Array.isArray(tableId) ? tableId.join(',') : tableId
  const res: AxiosResponse<Result<number>> = await request.delete(
    `${contextPath}/${ids}`
  )
  return res.data
}

// 生成代码（自定义路径）
export async function genCode(tableName: string): Promise<Result<string>> {
  const res: AxiosResponse<Result<string>> = await request.get(
    `${contextPath}/genCode/${tableName}`
  )
  return res.data
}

// 同步数据库
export async function synchDb(tableName: string): Promise<Result<number>> {
  const res: AxiosResponse<Result<number>> = await request.get(
    `${contextPath}/synchDb/${tableName}`
  )
  return res.data
}

// api/tool/gen.ts
export const batchGenCode = (tables: string) => {
  return request({
    url: `${contextPath}/batchGenCode`,
    method: 'get',
    params: { tables },
    responseType: 'blob',
    withCredentials: false
  })
}
