"use client";

import { Leaf } from "lucide-react";
import { BrapiEntityPage } from "@/components/brapi/BrapiEntityPage";
import { createCrop, deleteCrop, listCrops, updateCrop } from "@/services/dictionaryService";

const loadCropRows = async () => listCrops() as unknown as Record<string, unknown>[];

const createCropRow = async (payload: Record<string, unknown>) => createCrop({
  crop_name: String(payload.crop_name ?? ""),
}) as unknown as Record<string, unknown>;

const updateCropRow = async (id: string, payload: Record<string, unknown>) => updateCrop(id, {
  crop_name: String(payload.crop_name ?? ""),
}) as unknown as Record<string, unknown>;

const deleteCropRow = async (id: string) => {
  await deleteCrop(id);
};

export default function CropDictionaryPage() {
  return (
    <BrapiEntityPage
      icon={Leaf}
      iconBg="bg-gradient-to-br from-amber-500 to-orange-600"
      title="Crop 作物"
      description="多作物平台入口，所有业务对象均归属于特定作物"
      addLabel="新增作物"
      columns={[
        { key: "crop_name", label: "作物名称" },
      ]}
      fields={[
        { key: "crop_name", label: "作物名称 (Crop Name)", type: "text", required: true, placeholder: "如 Maize、Rice、Wheat" },
      ]}
      data={[]}
      stats={[{ label: "后端 crop 表", value: "API", className: "bg-amber-50 text-amber-700 dark:bg-amber-400/10 dark:text-amber-200" }]}
      loadData={loadCropRows}
      createRecord={createCropRow}
      updateRecord={updateCropRow}
      deleteRecord={deleteCropRow}
    />
  );
}
