import type { LocalizedString } from "@src/types/language"

// OS database entries

export enum OSTag {
  ABI1 = "abi1",
  ABI2 = "abi2",
  AtomicUpdates = "atomicity",
  CommercialSupport = "commercial_support",
  Community = "community",
  Container = "container",
  Desktop = "desktop",
  DIY = "diy",
  Embedded = "embedded",
  Firewall = "firewall",
  Immutable = "immutable",
  Rolling = "rolling",
  Router = "router",
  Server = "server",
  Virtualization = "virt",
  Linux = "linux",
  BSD = "bsd",
  Harmony = "harmony",
  RTOS = "rtos",
}

export interface OSInfoItem {
  name: LocalizedString
  href: string
  image: string
  description: LocalizedString
  tags: OSTag[]
}

// Chip database entries

export enum MarketSegment {
  Desktop = 1,
  Mobile = 2,
  Server = 3,
  Embedded = 4,
}

export interface ChipBasicInfo {
  name: string
  series: string
  market: MarketSegment
}

export interface ChipExtendedInfo {
  pic: string
  manual: string
}

export interface CPUSpecs {
  cores: number
  threads: number
  arch: string
  freq: string
  l1_inst_cache: string
  l1_data_cache: string
  l2_cache: string
  l3_cache: string
  voltage: string
  tpc: string
  tdp: string
}

export interface ChipIntegratedGPUSpecs {
  available: boolean
  name: string
}

export interface CPUMemorySpecs {
  max: string
  types: string
  channels: string
  ecc: boolean
}

export interface ChipInterfaceSpecs {
  io_name: string
  io_rev: string
  lanes: number | string
  usb_5gbps: number
  usb2: number
  sata: number
  eth: number | string
  spi: number
  uart: number
  i2c: number
  gpio: number
  avs: number
  d2d?: boolean
  d2d_name?: string | null
  other?: string
}

export interface ChipPackageSpecs {
  socket: string
  temperature: string
  t_case?: string
  t_junction?: string
  size: string
}

export interface CPUPowerSpecs {
  clock_gating: boolean
  frequency_scaling: boolean
  voltage_scaling: boolean
}

export interface CPUMicroarchitectureSpecs {
  isa: string
  isa_extensions: string[]
}

export interface ChipCommonInfo {
  basic: ChipBasicInfo
  notesPath?: string
  ext_info: ChipExtendedInfo
  exp: ChipInterfaceSpecs
  package: ChipPackageSpecs
}

export interface CPUInfoItem extends ChipCommonInfo {
  cpu: CPUSpecs
  gpu: ChipIntegratedGPUSpecs
  memory: CPUMemorySpecs
  power: CPUPowerSpecs
  technologies: CPUMicroarchitectureSpecs
}

export interface ChipsetSpecs {
  interface: string
}

export interface ChipsetInfoItem extends ChipCommonInfo {
  chipset: ChipsetSpecs
  gpu: ChipIntegratedGPUSpecs
}

export interface ChipInfoDB {
  cpu: { [key: string]: CPUInfoItem }
  chipset: { [key: string]: ChipsetInfoItem }
}

// Biweekly event entries

export type BiweeklyEventKind = "zhBiweekly" | "intlBiweekly"

export type BiweeklyResourceType =
  | "wemeet"
  | "kdocs"
  | "googledocs"
  | "zoom"
  | "zoomChat"
  | "bilibili"
  | "youtube"
  | "vkvideo"

export type BiweeklyResourceStatus = "available" | "wip" | "unavailable"

export interface BiweeklyResourceMetadata {
  label?: LocalizedString
  note?: LocalizedString
}

export interface BiweeklyAvailableResourceBase extends BiweeklyResourceMetadata {
  status?: "available"
}

export interface BiweeklyWemeetResource extends BiweeklyAvailableResourceBase {
  type: "wemeet"
  link: string
  number: string
}

export interface BiweeklyKDocsResource extends BiweeklyAvailableResourceBase {
  type: "kdocs"
  id: string
}

export interface BiweeklyGoogleDocsResource extends BiweeklyAvailableResourceBase {
  type: "googledocs"
  kind: string
  id: string
}

export interface BiweeklyZoomResource extends BiweeklyAvailableResourceBase {
  type: "zoom"
  link: string
  number?: string
  passcode?: string
}

export interface BiweeklyZoomChatResource extends BiweeklyAvailableResourceBase {
  type: "zoomChat"
  link: string
}

export interface BiweeklyBilibiliResource extends BiweeklyAvailableResourceBase {
  type: "bilibili"
  bvid?: string
  link?: string
}

export interface BiweeklyYoutubeResource extends BiweeklyAvailableResourceBase {
  type: "youtube"
  id: string
}

export interface BiweeklyVKVideoResource extends BiweeklyAvailableResourceBase {
  type: "vkvideo"
  id: string
}

export type BiweeklyAvailableResource =
  | BiweeklyWemeetResource
  | BiweeklyKDocsResource
  | BiweeklyGoogleDocsResource
  | BiweeklyZoomResource
  | BiweeklyZoomChatResource
  | BiweeklyBilibiliResource
  | BiweeklyYoutubeResource
  | BiweeklyVKVideoResource

export interface BiweeklyPendingResource extends BiweeklyResourceMetadata {
  type: BiweeklyResourceType
  status: "wip" | "unavailable"
}

export type BiweeklyResource =
  BiweeklyAvailableResource | BiweeklyPendingResource

export interface BiweeklyEventData {
  links: BiweeklyResource[]

  // NOTE: in JS and JSON one cannot have numbers as keys, so string is used
  // here. It is just the issue/session number in decimal.
  archives: { [key: string]: BiweeklyResource[] }
}

export interface BiweeklyDB {
  events: Record<BiweeklyEventKind, BiweeklyEventData>
}

// Download database entries

/**
 * 下载资源类型枚举。
 *
 * 添加新类型时，请同步更新 i18n 映射。
 */
export enum DownloadType {
  UEFIFirmware = "uefi-firmware",
  Datasheet = "datasheet",
  ReferenceManual = "reference-manual",
  UserManual = "user-manual",
  SchematicDiagram = "schematic-diagram",
  SDK = "sdk",
}

export interface DownloadItem {
  type: DownloadType
  version: string
  size: number
  date: string
  sha256: string
  url: string
  debug?: boolean
  preview?: boolean
  description?: LocalizedString
}

/**
 * A download item with its Markdown description pre-rendered to HTML at build
 * time. The `description` field is replaced by per-language `briefHtml` and
 * `detailHtml` fragments, split on the `<!-- truncate -->` marker.
 */
export interface RenderedDownloadItem {
  type: DownloadType
  version: string
  size: number
  date: string
  sha256: string
  url: string
  debug?: boolean
  preview?: boolean
  briefHtml?: LocalizedString
  detailHtml?: LocalizedString
}

export interface DownloadsDB {
  [resourceKey: string]: RenderedDownloadItem
}

// Device database entries

export interface DeviceInfoItem {
  name: LocalizedString
  family: string
  category: string
  image: string
  spec: string
  tags: string[]
  downloads?: string[]
}

export interface DeviceFamilyMeta {
  title: LocalizedString
}

export interface DeviceCategoryMeta {
  title: LocalizedString
}

export interface DeviceTagMeta {
  title: LocalizedString
}

export interface DeviceFamiliesConfig {
  families: { [key: string]: DeviceFamilyMeta }
  categories: { [key: string]: DeviceCategoryMeta }
  tags: { [key: string]: DeviceTagMeta }
}

export interface DeviceInfoDB {
  families: DeviceFamiliesConfig
  devices: { [key: string]: DeviceInfoItem }
}
