import type { SQL } from 'drizzle-orm'
import type { PageQuery } from './page'

/** save：有 id 则更新；insert：只插入 */
export type CrudUpsertMode = 'save' | 'insert'

/**
 * modifyBefore / modifyAfter 的操作名。
 * 内置：add | update | delete | restore | list | info | page；
 * 自定义方法可传任意字符串（如 settle）。
 */
export type CrudModifyType =
  | 'add'
  | 'update'
  | 'delete'
  | 'restore'
  | 'list'
  | 'info'
  | 'page'
  | (string & {})

/** add() 可选覆盖 */
export type CrudWriteOptions = {
  upsert?: CrudUpsertMode
}

/** 查询回收站范围 */
export type CrudTrashQueryOptions = {
  withTrashed?: boolean
  onlyTrashed?: boolean
  softDelete?: boolean
}

/** 删除选项 */
export type CrudDeleteOptions = {
  force?: boolean
  softDelete?: boolean
}

/** vome.crud 运行时配置 */
export type CrudConfig = {
  upsert?: CrudUpsertMode
  softDelete?: boolean
}

/** add 写入体（单条或批量；语义对齐当前 unknown，便于覆写签名） */
export type CrudAddData = unknown

/** update 第一参：SQL where / 单行对象 / 批量行 */
export type CrudUpdateWhere =
  | SQL
  | Record<string, unknown>
  | Record<string, unknown>[]

/** update 第二参：SQL 条件下的 patch */
export type CrudUpdatePatch = unknown

/** id 或 id 列表 */
export type CrudIdOrIds = number | string | Array<number | string>

/** delete / restore 条件 */
export type CrudDeleteWhere = SQL | CrudIdOrIds

/** list where */
export type CrudListWhere = SQL | undefined

/** list 选项 */
export type CrudListOptions = CrudTrashQueryOptions & {
  orderBy?: SQL[]
}

/** info：主键或 where */
export type CrudInfoWhere = number | string | SQL

/** page 查询 */
export type CrudPageQuery = PageQuery & {
  where?: SQL
  orderBy?: SQL[]
} & CrudTrashQueryOptions
