"use client";

// 개발관리 > PART 조회 (M2) — wace partMngList.jsp 1:1
// 그리드: status = 'release' 인 PART 23셀 + BOM_QTY
// 액션: 등록 / 수정 / 삭제 / 조회  (도면연동/ERP/Excel은 별 PR)
// 참조: docs/migration/development/01-part.md

import React, { useCallback, useEffect, useMemo, useState } from "react";
import { Button } from "@/components/ui/button";
import { PageHeader } from "@/components/common/PageHeader";
import { CompactFilterBar, CompactFilterField } from "@/components/common/CompactFilterBar";
import {
  Plus, Pencil, Trash2, FileSpreadsheet,
} from "lucide-react";
import { toast } from "sonner";
import { DataGrid, DataGridColumn } from "@/components/common/DataGrid";
import { devPartApi, PartListFilter, PartRow } from "@/lib/api/devPart";
import { PartFormDialog } from "@/components/development/PartFormDialog";
import { PartDetailDialog } from "@/components/development/PartDetailDialog";
import { PartExcelImportDialog } from "@/components/development/PartExcelImportDialog";
import { PartDrawingMultiUploadButton } from "@/components/development/PartDrawingMultiUploadButton";
import { DevPartSelect } from "@/components/development/DevPartSelect";
import { exportToExcel } from "@/lib/utils/excelExport";

const GRID_COLUMNS: DataGridColumn[] = [
  { key: "part_no", label: "품번", width: "w-[140px]", frozen: true },
  { key: "part_name", label: "품명", minWidth: "min-w-[220px]" },
  { key: "cu01_cnt", label: "3D",  width: "w-[80px]", align: "center", renderType: "folder" },
  { key: "cu02_cnt", label: "2D",  width: "w-[80px]", align: "center", renderType: "folder" },
  { key: "cu03_cnt", label: "PDF", width: "w-[80px]", align: "center", renderType: "folder" },
  { key: "material", label: "재료", width: "w-[100px]" },
  { key: "heat_treatment_hardness", label: "열처리경도", width: "w-[125px]" },
  { key: "heat_treatment_method", label: "열처리방법", width: "w-[125px]" },
  { key: "surface_treatment", label: "표면처리", width: "w-[115px]" },
  { key: "maker", label: "메이커", width: "w-[100px]" },
  { key: "part_type_title", label: "범주", width: "w-[100px]" },
  { key: "spec", label: "규격", width: "w-[140px]" },
  { key: "acctfg_nm", label: "계정구분", width: "w-[115px]", align: "center" },
  { key: "odrfg_nm", label: "조달구분", width: "w-[115px]", align: "center" },
  { key: "unit_dc_nm", label: "재고단위", width: "w-[115px]", align: "center" },
  { key: "unitmang_dc_nm", label: "관리단위", width: "w-[115px]", align: "center" },
  { key: "unitchng_nb", label: "환산수량", width: "w-[115px]", align: "right", formatNumber: true },
  { key: "lot_fg_nm", label: "LOT구분", width: "w-[115px]", align: "center" },
  { key: "use_yn_nm", label: "사용여부", width: "w-[115px]", align: "center" },
  { key: "qc_fg_nm", label: "검사여부", width: "w-[115px]", align: "center" },
  { key: "setitem_fg_nm", label: "SET품여부", width: "w-[120px]", align: "center" },
  { key: "req_fg_nm", label: "의뢰여부", width: "w-[115px]", align: "center" },
  { key: "unit_length", label: "개당길이", width: "w-[115px]", align: "right" },
  { key: "unit_qty", label: "개당수량", width: "w-[115px]", align: "right" },
  // M2 추가
  { key: "bom_qty", label: "BOM 수량", width: "w-[115px]", align: "right", formatNumber: true },
  { key: "revision", label: "REV", width: "w-[80px]", align: "center" },
  { key: "eo_no", label: "EO_NO", width: "w-[120px]" },
];

const EMPTY_FILTER: PartListFilter = {
  search_part_no: "", search_part_name: "",
  page: 1, page_size: 50,
};

export default function PartSearchPage() {
  const [rows, setRows] = useState<PartRow[]>([]);
  const [total, setTotal] = useState(0);
  const [loading, setLoading] = useState(false);
  const [filter, setFilter] = useState<PartListFilter>(EMPTY_FILTER);
  const [checkedIds, setCheckedIds] = useState<string[]>([]);

  const [formOpen, setFormOpen] = useState(false);
  const [formMode, setFormMode] = useState<"create" | "edit">("create");
  const [formObjid, setFormObjid] = useState<string | null>(null);
  const [detailOpen, setDetailOpen] = useState(false);
  const [detailObjid, setDetailObjid] = useState<string | null>(null);
  const [excelOpen, setExcelOpen] = useState(false);

  const fetchList = useCallback(async (override?: Partial<PartListFilter>) => {
    setLoading(true);
    try {
      const f = { ...filter, ...override };
      const res = await devPartApi.list(f);
      setRows(res.rows ?? []);
      setTotal(res.total ?? 0);
      setCheckedIds([]);
    } catch (e: any) {
      toast.error(e?.response?.data?.message ?? e?.message ?? "조회 실패");
    } finally {
      setLoading(false);
    }
  }, [filter]);

  useEffect(() => { fetchList(); /* eslint-disable-next-line */ }, []);

  const columns = useMemo(
    () => GRID_COLUMNS.map((c) =>
      c.key === "part_no"
        ? { ...c, onClick: (row: any) => { setDetailObjid(row.objid); setDetailOpen(true); } }
        : c,
    ),
    [],
  );

  // DataGrid 는 row.id 를 키로 사용 — backend 응답은 row.objid 이므로 매핑
  const gridRows = useMemo(() => rows.map((r) => ({ ...r, id: r.objid })), [rows]);

  // ─── 하단 통계 ──────────────────────────────────────────────
  // PART 건수(총·페이지) / BOM 수량·환산수량·개당수량 합계
  const partSummary = useMemo(() => {
    const pageCount = gridRows.length;
    const bomQty   = gridRows.reduce((acc, r: any) => acc + Number(r.bom_qty || 0), 0);
    const unitChng = gridRows.reduce((acc, r: any) => acc + Number(r.unitchng_nb || 0), 0);
    const unitQty  = gridRows.reduce((acc, r: any) => acc + Number(r.unit_qty || 0), 0);
    const intFmt = (n: number) => n.toLocaleString();
    return [
      { label: "전체 건수", value: intFmt(total), suffix: "건" },
      { label: "페이지 건수", value: intFmt(pageCount), suffix: "건" },
      { label: "BOM 수량 합계", value: intFmt(bomQty) },
      { label: "환산수량 합계", value: intFmt(unitChng) },
      { label: "개당수량 합계", value: intFmt(unitQty) },
    ];
  }, [gridRows, total]);

  const handleCreate = () => {
    setFormMode("create"); setFormObjid(null); setFormOpen(true);
  };
  const handleEdit = () => {
    if (checkedIds.length !== 1) return toast.error("수정할 행 1개를 선택하세요.");
    setFormMode("edit"); setFormObjid(checkedIds[0]); setFormOpen(true);
  };
  const handleDelete = async () => {
    if (checkedIds.length === 0) return toast.error("선택된 행이 없습니다.");
    if (!confirm(`${checkedIds.length}건을 삭제하시겠습니까?`)) return;
    try {
      const res = await devPartApi.remove(checkedIds);
      toast.success(res?.message ?? "삭제되었습니다.");
      fetchList();
    } catch (e: any) {
      toast.error(e?.response?.data?.message ?? e?.message ?? "삭제 실패");
    }
  };
  const handleEditFromDetail = (objid: string) => {
    setDetailOpen(false);
    setFormMode("edit"); setFormObjid(objid); setFormOpen(true);
  };

  return (
    <div className="flex h-full flex-col overflow-hidden p-2 gap-2">
      <PageHeader
        loading={loading}
        onSearch={() => fetchList()}
        onReset={() => { setFilter(EMPTY_FILTER); fetchList(EMPTY_FILTER); }}
        actions={
        <>
          <Button size="sm" className="h-8 gap-1 text-xs" onClick={handleCreate}>
            <Plus className="h-3.5 w-3.5" />등록
          </Button>
          <Button size="sm" variant="secondary" className="h-8 gap-1 text-xs" onClick={handleEdit}
                  disabled={checkedIds.length !== 1}>
            <Pencil className="h-3.5 w-3.5" />수정
          </Button>
          <Button size="sm" variant="destructive" className="h-8 gap-1 text-xs" onClick={handleDelete}
                  disabled={checkedIds.length === 0}>
            <Trash2 className="h-3.5 w-3.5" />삭제
          </Button>
          <Button size="sm" variant="outline" className="h-8 gap-1 text-xs" onClick={() => setExcelOpen(true)}>
            <FileSpreadsheet className="h-3.5 w-3.5" />Excel Upload
          </Button>
          {/* M2 조회 — partNoList 미전달: IS_LAST='1' 전체 part_mng 매칭 (페이지 밖도 허용) */}
          <PartDrawingMultiUploadButton onUploaded={() => fetchList()} />
        </>
      } />

      <CompactFilterBar totalText={<>총 {total.toLocaleString()}건 (M2: status = 'release')</>}>
        <CompactFilterField label="품번" width={220}>
          <DevPartSelect mode="partNo"
                         value={filter.search_part_no ?? ""}
                         onValueChange={(v, row) => setFilter((prev) => ({
                           ...prev,
                           search_part_no: v,
                           search_part_name: row?.part_name ?? prev.search_part_name,
                         }))} />
        </CompactFilterField>
        <CompactFilterField label="품명" width={220}>
          <DevPartSelect mode="partName"
                         value={filter.search_part_name ?? ""}
                         onValueChange={(v, row) => setFilter((prev) => ({
                           ...prev,
                           search_part_name: v,
                           search_part_no: row?.part_no ?? prev.search_part_no,
                         }))} />
        </CompactFilterField>
      </CompactFilterBar>

      <DataGrid
        columns={columns}
        data={gridRows}
        loading={loading}
        showRowNumber
        showCheckbox
        checkedIds={checkedIds}
        onCheckedChange={setCheckedIds}
        emptyMessage="조건에 맞는 PART가 없습니다."
        gridId="development-part-search"
        showColumnSettings
        paginationStyle="range"
        pageSizeOptions={[10, 15, 20, 50, 100, 200, 500]}
        serverPaging
        serverPage={filter.page ?? 1}
        serverPageSize={filter.page_size ?? 50}
        serverTotalItems={total}
        onPageChange={(p) => { setFilter(f => ({ ...f, page: p })); fetchList({ page: p }); }}
        onPageSizeChange={(n) => { setFilter(f => ({ ...f, page: 1, page_size: n })); fetchList({ page: 1, page_size: n }); }}
        summaryStats={partSummary}
        systemColumnKeys={["revision", "eo_no"]}
        onRefresh={() => fetchList()}
        onDownload={() => {
          if (gridRows.length === 0) { toast.info("내보낼 데이터가 없습니다."); return; }
          const exportRows = gridRows.map((r: any) => {
            const out: Record<string, any> = {};
            GRID_COLUMNS.forEach((col) => { out[col.label] = r[col.key] ?? ""; });
            return out;
          });
          exportToExcel(exportRows, "PART_조회.xlsx", "PART_조회");
        }}
        showChart
      />

      <PartFormDialog
        open={formOpen}
        onOpenChange={setFormOpen}
        mode={formMode}
        editObjid={formObjid}
        onSaved={fetchList}
      />
      <PartDetailDialog
        open={detailOpen}
        onOpenChange={setDetailOpen}
        objid={detailObjid}
        onEdit={handleEditFromDetail}
      />
      <PartExcelImportDialog
        open={excelOpen}
        onOpenChange={setExcelOpen}
        onSaved={fetchList}
      />
    </div>
  );
}
