"use client";

import { GitBranch } from "lucide-react";
import { BrapiEntityPage } from "@/components/brapi/BrapiEntityPage";
import {
  createBreedingMethodRow,
  deleteBreedingMethodRow,
  fetchBreedingMethodRows,
  updateBreedingMethodRow,
} from "./api";

const loadBreedingMethodRows = async () => fetchBreedingMethodRows() as unknown as Record<string, unknown>[];

export default function BreedingMethodPage() {
  return (
    <BrapiEntityPage
      icon={GitBranch}
      iconBg="bg-gradient-to-br from-amber-500 to-orange-600"
      title="Breeding Method 育种方法"
      description="维护 breeding_method 字典，说明种质材料的形成方式（杂交、回交、DH 等）"
      addLabel="新增育种方法"
      useEnhancedDialog
      columns={[
        { key: "breedingMethodDbId", label: "方法 ID" },
        { key: "name", label: "方法名称" },
        { key: "abbreviation", label: "缩写" },
        {
          key: "description",
          label: "描述",
          render: (value) => {
            const text = String(value ?? "").trim();
            if (!text) return "—";
            return text.length > 48 ? `${text.slice(0, 48)}…` : text;
          },
        },
      ]}
      fields={[
        {
          key: "breedingMethodName",
          label: "方法名称 (Name)",
          type: "text",
          required: true,
          placeholder: "如 Male Backcross / Doubled Haploid / 回交选育",
        },
        {
          key: "abbreviation",
          label: "缩写 (Abbreviation)",
          type: "text",
          placeholder: "如 MB / BC / DH",
        },
        {
          key: "description",
          label: "描述 (Description)",
          type: "textarea",
          placeholder: "如：回交用于恢复目标基因",
          colSpan: 2,
        },
      ]}
      data={[]}
      stats={[
        {
          label: "/brapi/v2/breedingmethods",
          value: "BrAPI",
          className: "bg-amber-50 text-amber-700 dark:bg-amber-400/10 dark:text-amber-200",
        },
      ]}
      loadData={loadBreedingMethodRows}
      createRecord={(payload) => createBreedingMethodRow(payload) as unknown as Promise<Record<string, unknown>>}
      updateRecord={(id, payload) => updateBreedingMethodRow(id, payload) as unknown as Promise<Record<string, unknown>>}
      deleteRecord={deleteBreedingMethodRow}
    />
  );
}
