"use client";

import { useCallback } from "react";
import { MapPin } from "lucide-react";
import { BrapiEntityPage } from "@/components/brapi/BrapiEntityPage";
import { createLocationRow, fetchLocationDetail, fetchLocationRows, deleteLocationRow, updateLocationRow } from "./api";

const locationFields = [
  { key: "locationName", label: "地点名称", type: "text" as const, required: true, placeholder: "如 Beijing Research Station" },
  { key: "locationType", label: "地点类型", type: "text" as const, placeholder: "如 试验站 / 温室" },
  { key: "countryCode", label: "国家代码", type: "text" as const, placeholder: "CHN" },
  { key: "countryName", label: "国家名称", type: "text" as const, placeholder: "China" },
  { key: "abbreviation", label: "缩写", type: "text" as const, placeholder: "BRS" },
  { key: "environmentType", label: "环境类型", type: "text" as const, placeholder: "Field / Greenhouse" },
  { key: "exposure", label: "光照/暴露", type: "text" as const },
  { key: "instituteName", label: "机构名称", type: "text" as const },
  { key: "instituteAddress", label: "机构地址", type: "text" as const, colSpan: 2 as const },
  { key: "parentLocationDbId", label: "父级地点 ID", type: "text" as const },
  { key: "parentLocationName", label: "父级地点名称", type: "text" as const },
  { key: "siteStatus", label: "地点状态", type: "text" as const, placeholder: "ACTIVE" },
  { key: "slope", label: "坡度", type: "text" as const },
  { key: "topography", label: "地形", type: "text" as const },
  { key: "coordinateDescription", label: "坐标描述", type: "text" as const, colSpan: 2 as const },
  { key: "coordinateUncertainty", label: "坐标不确定性", type: "text" as const },
  { key: "documentationURL", label: "文档链接", type: "text" as const, colSpan: 2 as const },
];

export default function LocationDictionaryPage() {
  const loadLocationRows = useCallback(async () => {
    const rows = await fetchLocationRows();
    return rows as unknown as Record<string, unknown>[];
  }, []);

  const loadLocationRecord = useCallback(async (locationDbId: string) => {
    const row = await fetchLocationDetail(locationDbId);
    return row as unknown as Record<string, unknown>;
  }, []);

  const updateLocationRecord = useCallback(async (id: string, payload: Record<string, unknown>) => {
    const row = await updateLocationRow(id, payload);
    return row as unknown as Record<string, unknown>;
  }, []);

  const createLocationRecord = useCallback(async (payload: Record<string, unknown>) => {
    const row = await createLocationRow(payload);
    return row as unknown as Record<string, unknown>;
  }, []);

  return (
    <BrapiEntityPage
      icon={MapPin}
      iconBg="bg-gradient-to-br from-green-500 to-teal-600"
      title="Location 地点"
      description="维护试验站、田块、温室、实验室等物理地点"
      addLabel="新增地点"
      columns={[
        { key: "locationDbId", label: "Location DbId" },
        { key: "locationName", label: "地点名称" },
        { key: "locationType", label: "地点类型" },
        { key: "countryCode", label: "国家代码" },
        { key: "instituteName", label: "机构名称" },
        { key: "siteStatus", label: "地点状态" },
      ]}
      fields={locationFields}
      data={[]}
      stats={[{ label: "/brapi/v2/locations", value: "BrAPI", className: "bg-green-50 text-green-700 dark:bg-green-400/10 dark:text-green-200" }]}
      loadData={loadLocationRows}
      fetchRecord={loadLocationRecord}
      createRecord={createLocationRecord}
      updateRecord={updateLocationRecord}
      deleteRecord={deleteLocationRow}
    />
  );
}
