import { CrudPage } from '../components/Crud'
import { Badge, TENANTS } from '../components/ui'
import { getFirmware, createFirmware, deleteFirmware } from '../api/client'

// 펌웨어 배포 — eESB(ESL_DEVICE) / 게이트웨이(GATEWAY) / 허브(HUB) 펌웨어 버전 관리.
const DEV = [
  { value: 'ESL_DEVICE', label: 'eESB (간판 태그)' },
  { value: 'GATEWAY', label: 'GW (게이트웨이)' },
  { value: 'HUB', label: 'HUB (허브)' },
]

export default function FirmwareList() {
  return (
    <CrudPage
      title="펌웨어 배포"
      subtitle="eESB · 게이트웨이 · 허브 펌웨어(FW) 버전 등록 및 중앙배포"
      addLabel="펌웨어 등록"
      queryKey={['firmware']}
      fetcher={() => getFirmware()}
      onCreate={createFirmware} onDelete={deleteFirmware}
      defaults={{ tenantCode: 'ZIOINFO', deviceType: 'ESL_DEVICE', latest: true }}
      columns={[
        { key: 'firmwareVersion', header: '버전', render: r => <span className="font-mono text-xs text-white">{r.firmwareVersion}</span> },
        { key: 'deviceType', header: '장치', render: r => <Badge tone="blue">{r.deviceType}</Badge> },
        { key: 'fileName', header: '파일', render: r => <span className="text-xs text-gray-300">{r.fileName}</span> },
        { key: 'description', header: '설명', render: r => <span className="text-xs text-gray-400">{r.description}</span> },
        { key: 'latest', header: '최신', align: 'center', render: r => r.latest ? <Badge tone="green">LATEST</Badge> : '' },
      ]}
      form={[
        { key: 'tenantCode', label: '테넌트', type: 'select', opts: TENANTS },
        { key: 'firmwareVersion', label: '펌웨어 버전', placeholder: '예: 2.06' },
        { key: 'deviceType', label: '대상 장치', type: 'select', opts: DEV },
        { key: 'fileName', label: '파일명', placeholder: 'eESB_2.06.bin' },
        { key: 'filePath', label: '파일 경로' },
        { key: 'checksum', label: '체크섬(MD5)' },
        { key: 'description', label: '설명' },
        { key: 'latest', label: '최신 버전으로 지정', type: 'checkbox' },
      ]}
    />
  )
}
