"use client";

import { z } from "zod";
import { CrudPage } from "@/components/crud-page";
import { Equipment } from "@/lib/api";

const schema = z.object({
  kind: z.enum(["temperature_logger", "pressure_logger", "sensor"]),
  manufacturer: z.string().optional().nullable(),
  model: z.string().optional().nullable(),
  serial_number: z.string().min(2),
  calibrated_on: z.string().optional().nullable(),
  calibration_due_on: z.string().optional().nullable(),
  certificate_document_id: z.string().optional().nullable(),
  status: z.enum(["green", "yellow", "red"])
});

export default function EquipmentPage() {
  return (
    <CrudPage<Equipment>
      title="Pruefmittel"
      subtitle="Temperaturlogger, Drucklogger, Sensoren, Kalibrierstatus und Zertifikate."
      endpoint="/equipment"
      columns={[
        { key: "kind", label: "Art" },
        { key: "manufacturer", label: "Hersteller" },
        { key: "model", label: "Modell" },
        { key: "serial_number", label: "Seriennummer" },
        { key: "calibration_due_on", label: "Gueltig bis" },
        { key: "status", label: "Status" }
      ]}
      fields={[
        { name: "kind", label: "Art", type: "select", options: [{ label: "Temperaturlogger", value: "temperature_logger" }, { label: "Drucklogger", value: "pressure_logger" }, { label: "Sensor", value: "sensor" }] },
        { name: "manufacturer", label: "Hersteller" },
        { name: "model", label: "Modell" },
        { name: "serial_number", label: "Seriennummer" },
        { name: "calibrated_on", label: "Kalibrierung", type: "date" },
        { name: "calibration_due_on", label: "Kalibrierung gueltig bis", type: "date" },
        { name: "certificate_document_id", label: "Kalibrierzertifikat Dokument-ID" },
        { name: "status", label: "Ampel", type: "select", options: [{ label: "Gruen", value: "green" }, { label: "Gelb", value: "yellow" }, { label: "Rot", value: "red" }] }
      ]}
      schema={schema}
      emptyValues={{ kind: "temperature_logger", manufacturer: "", model: "", serial_number: "", calibrated_on: "", calibration_due_on: "", certificate_document_id: "", status: "green" }}
    />
  );
}

