export type SimpleFieldCategory = 'siteFields' | 'trackFields' | 'planFields' | 'carFields'

export const SIMPLE_FIELD_CATEGORIES: { key: SimpleFieldCategory; label: string }[] = [
  { key: 'siteFields', label: '站点字段 (siteFields)' },
  { key: 'trackFields', label: '路径字段 (trackFields)' },
  { key: 'planFields', label: '计划字段 (planFields)' },
  { key: 'carFields', label: '车辆字段 (carFields)' }
]

export interface SimpleFieldRecord {
  id: string
  carType: string
  fieldType: string
  key: string
  value: string
  dataType: string
  chinese: string | null
  english: string | null
  other: string
  isDefault: boolean
  createTime: string
  updateTime: string
}

export interface SimpleFieldPayload {
  id?: string
  carType: string
  fieldType: string
  key: string
  value?: string
  dataType?: string
  chinese?: string | null
  english?: string | null
  other?: string
  isDefault: boolean
}

export interface CoderFieldDef {
  name: string
  typeName: string
  defaultValue: unknown
}

export interface CoderFieldGroup {
  typeName: string
  shortName: string
  assemblyName: string
  baseTypeName?: string
  fields: CoderFieldDef[]
}

export interface CarTypeCoderFields {
  typeName: string
  shortName: string
  label: string
  assemblyName: string
  siteFields: CoderFieldGroup
  trackFields: CoderFieldGroup
  planFields: CoderFieldGroup
  carFields: CoderFieldGroup
}

export interface SimpleFieldBatchPayload {
  replaceAll: boolean
  items: SimpleFieldPayload[]
}

export interface FieldRow {
  rowKey: string
  id?: string
  carType: string
  fieldType: SimpleFieldCategory
  key: string
  dataType: string
  value: string
  chinese: string | null
  english: string | null
  other: string
  isDefault: boolean
}

export const ALL_CAR_TYPES = '__all__'

/** car_type 唯一标识：assemblyName.shortName */
export function buildCarType(assemblyName: string, shortName: string): string {
  const asm = assemblyName?.trim() ?? ''
  const sn = shortName?.trim() ?? ''
  return asm && sn ? `${asm}.${sn}` : sn || asm
}
