/**
 * 简单资源 Schema 注册表：驱动通用 list/form，消灭默认 4 字段 + JSON 主路径。
 * 复杂模块（posts/works/videos/attachments/settings/about/inquiries）走专用页，不在此注册。
 */

export type FieldType =
  | 'text'
  | 'textarea'
  | 'number'
  | 'switch'
  | 'select'
  | 'media'
  | 'rating'

export interface SchemaField {
  key: string
  label: string
  type: FieldType
  placeholder?: string
  /** select 选项 */
  options?: { label: string, value: string | number }[]
  /** 只读（如系统模板 slug） */
  readonly?: boolean
  /** 编辑态才显示 */
  editOnly?: boolean
}

export interface SchemaFilter {
  key: string
  label: string
  type: 'text' | 'select'
  options?: { label: string, value: string | number }[]
}

export interface ResourceSchema {
  title: string
  /** 真只读：无新建/删除/编辑，点行开详情抽屉 */
  readonly?: boolean
  /** 禁止编辑（可新建删除），如邮箱后缀 */
  noEdit?: boolean
  /** 页内新建（邮箱后缀） */
  inlineCreate?: boolean
  /** 服务端分页 */
  serverPaging?: boolean
  /** 默认 pageSize */
  pageSize?: number
  /** 列表主标题字段候选 */
  titleKeys?: string[]
  /** 列表副标题字段候选 */
  subtitleKeys?: string[]
  /** 封面/头像字段 */
  coverKey?: string
  /** 状态字段（用于徽章） */
  statusKey?: string
  /** 状态文案映射 */
  statusMap?: Record<string, string>
  filters?: SchemaFilter[]
  fields: SchemaField[]
  /** 本地关键词过滤字段 */
  localSearchKeys?: string[]
}

const CODE_TYPE_OPTIONS = [
  { label: '前端', value: 0 },
  { label: '后端', value: 1 },
  { label: '其他', value: 2 },
]

/** 简单资源 Schema；未列出的走兜底 */
export const RESOURCE_SCHEMAS: Record<string, ResourceSchema> = {
  categories: {
    title: '分类管理',
    titleKeys: ['name'],
    subtitleKeys: ['slug', 'sortOrder'],
    localSearchKeys: ['name', 'slug'],
    fields: [
      { key: 'name', label: '名称', type: 'text' },
      { key: 'slug', label: 'Slug', type: 'text' },
      { key: 'sortOrder', label: '排序', type: 'number' },
      { key: 'description', label: '描述', type: 'textarea' },
    ],
  },
  columns: {
    title: '专栏管理',
    titleKeys: ['name', 'title'],
    subtitleKeys: ['description', 'sortOrder'],
    coverKey: 'cover',
    statusKey: 'isActive',
    statusMap: { '1': '启用', '0': '停用', true: '启用', false: '停用' },
    filters: [
      { key: 'keyword', label: '关键词', type: 'text' },
      {
        key: 'isActive',
        label: '状态',
        type: 'select',
        options: [
          { label: '全部', value: '' },
          { label: '启用', value: 1 },
          { label: '停用', value: 0 },
        ],
      },
    ],
    localSearchKeys: ['name', 'title', 'description'],
    fields: [
      { key: 'name', label: '名称', type: 'text' },
      { key: 'cover', label: '封面', type: 'media' },
      { key: 'isActive', label: '启用', type: 'switch' },
      { key: 'sortOrder', label: '排序', type: 'number' },
      { key: 'description', label: '描述', type: 'textarea' },
    ],
  },
  'video-categories': {
    title: '视频分类',
    titleKeys: ['name'],
    subtitleKeys: ['slug', 'sortOrder'],
    localSearchKeys: ['name', 'slug'],
    fields: [
      { key: 'name', label: '名称', type: 'text' },
      { key: 'slug', label: 'Slug', type: 'text' },
      { key: 'sortOrder', label: '排序', type: 'number' },
      { key: 'description', label: '描述', type: 'textarea' },
    ],
  },
  'video-albums': {
    title: '视频专辑',
    titleKeys: ['name'],
    subtitleKeys: ['categoryName', 'videoCount'],
    coverKey: 'cover',
    statusKey: 'isActive',
    statusMap: { '1': '启用', '0': '停用', true: '启用', false: '停用' },
    localSearchKeys: ['name', 'description'],
    fields: [
      { key: 'name', label: '名称', type: 'text' },
      { key: 'categoryId', label: '分类ID', type: 'number' },
      { key: 'cover', label: '封面', type: 'media' },
      { key: 'sortOrder', label: '排序', type: 'number' },
      { key: 'isActive', label: '启用', type: 'switch' },
      { key: 'description', label: '描述', type: 'textarea' },
    ],
  },
  snippets: {
    title: '代码片段',
    titleKeys: ['title'],
    subtitleKeys: ['description'],
    localSearchKeys: ['title', 'description'],
    fields: [
      { key: 'title', label: '标题', type: 'text' },
      { key: 'codeTypeId', label: '分类ID', type: 'number' },
      { key: 'code', label: '代码', type: 'textarea' },
      { key: 'description', label: '描述', type: 'textarea' },
    ],
  },
  'code-types': {
    title: '代码分类',
    titleKeys: ['name'],
    subtitleKeys: ['category'],
    localSearchKeys: ['name'],
    fields: [
      { key: 'name', label: '名称', type: 'text' },
      { key: 'category', label: '类别', type: 'select', options: CODE_TYPE_OPTIONS },
    ],
  },
  tags: {
    title: '标签管理',
    titleKeys: ['name'],
    subtitleKeys: ['slug'],
    localSearchKeys: ['name', 'slug'],
    fields: [
      { key: 'name', label: '名称', type: 'text' },
      { key: 'slug', label: 'Slug', type: 'text' },
    ],
  },
  testimonials: {
    title: '客户评价',
    titleKeys: ['name'],
    subtitleKeys: ['role', 'rating'],
    coverKey: 'avatar',
    filters: [
      { key: 'keyword', label: '关键词', type: 'text' },
      {
        key: 'rating',
        label: '评分',
        type: 'select',
        options: [
          { label: '全部', value: '' },
          { label: '5星', value: 5 },
          { label: '4星', value: 4 },
          { label: '3星', value: 3 },
        ],
      },
    ],
    localSearchKeys: ['name', 'role', 'content'],
    fields: [
      { key: 'name', label: '姓名', type: 'text' },
      { key: 'role', label: '职位', type: 'text' },
      { key: 'avatar', label: '头像', type: 'media' },
      { key: 'rating', label: '评分', type: 'rating' },
      { key: 'content', label: '评价内容', type: 'textarea' },
    ],
  },
  partners: {
    title: '合作伙伴',
    titleKeys: ['name'],
    subtitleKeys: ['url', 'description'],
    coverKey: 'logo',
    localSearchKeys: ['name', 'description', 'url'],
    fields: [
      { key: 'name', label: '名称', type: 'text' },
      { key: 'logo', label: 'Logo', type: 'media' },
      { key: 'url', label: '链接', type: 'text' },
      { key: 'description', label: '描述', type: 'textarea' },
    ],
  },
  'email-suffixes': {
    title: '邮箱配置',
    noEdit: true,
    inlineCreate: true,
    titleKeys: ['suffix'],
    subtitleKeys: ['sortOrder', 'isActive'],
    statusKey: 'isActive',
    statusMap: { '1': '启用', '0': '停用', true: '启用', false: '停用' },
    localSearchKeys: ['suffix'],
    fields: [
      { key: 'suffix', label: '后缀', type: 'text', placeholder: '@example.com' },
      { key: 'sortOrder', label: '排序', type: 'number' },
      { key: 'isActive', label: '启用', type: 'switch' },
    ],
  },
  users: {
    title: '用户管理',
    serverPaging: true,
    pageSize: 10,
    titleKeys: ['username'],
    subtitleKeys: ['email', 'role'],
    coverKey: 'avatar',
    statusKey: 'isActive',
    statusMap: { '1': '启用', '0': '停用', true: '启用', false: '停用' },
    filters: [
      { key: 'keyword', label: '关键词', type: 'text' },
      {
        key: 'role',
        label: '角色',
        type: 'select',
        options: [
          { label: '全部', value: '' },
          { label: 'admin', value: 'admin' },
          { label: 'editor', value: 'editor' },
          { label: 'viewer', value: 'viewer' },
        ],
      },
      {
        key: 'isActive',
        label: '状态',
        type: 'select',
        options: [
          { label: '全部', value: '' },
          { label: '启用', value: 1 },
          { label: '停用', value: 0 },
        ],
      },
    ],
    fields: [
      { key: 'username', label: '用户名', type: 'text' },
      { key: 'email', label: '邮箱', type: 'text' },
      { key: 'avatar', label: '头像', type: 'media' },
      {
        key: 'role',
        label: '角色',
        type: 'select',
        options: [
          { label: 'admin', value: 'admin' },
          { label: 'editor', value: 'editor' },
          { label: 'viewer', value: 'viewer' },
        ],
      },
      { key: 'isActive', label: '启用', type: 'switch' },
      { key: 'bio', label: '简介', type: 'textarea' },
      { key: 'phone', label: '手机', type: 'text' },
      { key: 'wechat', label: '微信', type: 'text' },
      { key: 'wechatQrcode', label: '微信二维码', type: 'media' },
    ],
  },
  roles: {
    title: '角色管理',
    titleKeys: ['name'],
    subtitleKeys: ['description'],
    localSearchKeys: ['name', 'description'],
    fields: [
      { key: 'name', label: '名称', type: 'text' },
      { key: 'description', label: '描述', type: 'textarea' },
    ],
  },
  'attachment-categories': {
    title: '附件分类',
    titleKeys: ['name'],
    subtitleKeys: ['sortOrder', 'description'],
    localSearchKeys: ['name', 'description'],
    fields: [
      { key: 'name', label: '名称', type: 'text' },
      { key: 'sortOrder', label: '排序', type: 'number' },
      { key: 'description', label: '描述', type: 'textarea' },
    ],
  },
  'ppt-templates': {
    title: 'PPT 模板',
    titleKeys: ['name'],
    subtitleKeys: ['slug', 'sortOrder'],
    statusKey: 'isActive',
    statusMap: { '1': '启用', '0': '停用', true: '启用', false: '停用' },
    localSearchKeys: ['name', 'slug'],
    fields: [
      { key: 'name', label: '名称', type: 'text' },
      { key: 'slug', label: 'Slug', type: 'text' },
      { key: 'description', label: '描述', type: 'textarea' },
      { key: 'isDefault', label: '默认', type: 'switch' },
      { key: 'isActive', label: '启用', type: 'switch' },
      { key: 'sortOrder', label: '排序', type: 'number' },
      { key: 'config', label: '配置 JSON', type: 'textarea' },
    ],
  },
  logs: {
    title: '操作日志',
    readonly: true,
    serverPaging: true,
    pageSize: 20,
    titleKeys: ['action', 'path'],
    subtitleKeys: ['username', 'createdAt', 'method', 'status'],
    filters: [
      { key: 'action', label: '操作', type: 'text' },
      { key: 'method', label: '方法', type: 'text' },
      { key: 'status', label: '状态码', type: 'text' },
    ],
    fields: [],
  },
  'access-logs': {
    title: '访问日志',
    readonly: true,
    serverPaging: true,
    pageSize: 20,
    titleKeys: ['path'],
    subtitleKeys: ['region', 'ip', 'createdAt', 'statusCode'],
    filters: [
      { key: 'path', label: '路径', type: 'text' },
      { key: 'region', label: '地区', type: 'text' },
    ],
    fields: [],
  },
  /** 已拆专用页，保留兜底标题 */
  posts: { title: '文章管理', fields: [], titleKeys: ['title'] },
  works: { title: '作品管理', fields: [], titleKeys: ['title'] },
  videos: { title: '视频列表', fields: [], titleKeys: ['title'] },
  attachments: { title: '附件库', readonly: true, fields: [], titleKeys: ['originalName', 'fileName'] },
  inquiries: { title: '合作咨询', readonly: true, fields: [], titleKeys: ['name'] },
  'oss-configs': { title: 'OSS配置', fields: [], titleKeys: ['name'] },
  about: { title: '关于页面', fields: [] },
  settings: { title: '全局配置', fields: [] },
}

export function getResourceSchema(resource: string): ResourceSchema {
  return RESOURCE_SCHEMAS[resource] || {
    title: resource || '资源管理',
    titleKeys: ['title', 'name', 'username'],
    subtitleKeys: ['description', 'createdAt'],
    fields: [
      { key: 'title', label: '标题', type: 'text' },
      { key: 'name', label: '名称', type: 'text' },
      { key: 'description', label: '描述', type: 'textarea' },
    ],
  }
}

/** 从记录中按候选 key 取展示文案 */
export function pickField(item: Record<string, unknown>, keys?: string[]): string {
  if (!keys?.length) return ''
  for (const k of keys) {
    const v = item[k]
    if (v != null && v !== '') return String(v)
  }
  return ''
}
