"use client";

// 개발관리 > PART 등록 (M1) — wace partMngTempList.jsp 1:1
// 그리드: status != 'release' 인 PART 23셀
// 액션: 등록 / 수정 / 삭제 / 확정 / 조회
// 참조: 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, CheckSquare, 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";

// wace 23셀 + 부속 (PARENT_PART_INFO/PARTNER_TITLE/Q_QTY)
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" },
  // M1 부속
  { key: "partner_title", label: "공급업체(시퀀스)", minWidth: "min-w-[180px]" },
  { key: "parent_part_info", label: "상위 품번", width: "w-[120px]" },
  { key: "q_qty", label: "수량(BOM)", width: "w-[115px]", align: "right" },
  { key: "revision", label: "REV", width: "w-[80px]", align: "center" },
  { key: "status", label: "상태", width: "w-[95px]", align: "center" },
];

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

export default function PartRegistPage() {
  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.listTemp(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 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 qQty     = gridRows.reduce((acc, r: any) => acc + Number(r.q_qty || 0), 0);
    const intFmt = (n: number) => n.toLocaleString();
    return [
      { label: "전체 건수", value: intFmt(total), suffix: "건" },
      { label: "페이지 건수", value: intFmt(pageCount), suffix: "건" },
      { label: "환산수량 합계", value: intFmt(unitChng) },
      { label: "개당수량 합계", value: intFmt(unitQty) },
      { label: "BOM 수량 합계", value: intFmt(qQty) },
    ];
  }, [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 ?? "삭제 실패");
    }
  };

  // 확정 (M1 → M2): EO_NO 채번 + part_mng_history 이력
  const handleDeploy = async () => {
    if (checkedIds.length === 0) return toast.error("확정할 행을 선택하세요.");
    if (!confirm(`${checkedIds.length}건을 확정하시겠습니까? (M1 → M2)`)) return;
    try {
      const res = await devPartApi.deploy(checkedIds);
      toast.success(`${res.deployed}건이 확정되었습니다.`);
      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>
          <PartDrawingMultiUploadButton
            partNoList={rows.map((r) => r.part_no).filter(Boolean) as string[]}
            onUploaded={() => fetchList()}
          />
          <Button size="sm" onClick={handleDeploy}
                  disabled={checkedIds.length === 0}
                  className="h-8 gap-1 bg-emerald-600 hover:bg-emerald-700 text-white text-xs">
            <CheckSquare className="h-3.5 w-3.5" />확정
          </Button>
        </>
      } />

      <CompactFilterBar totalText={<>총 {total.toLocaleString()}건 (M1: 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-regist"
        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", "status"]}
        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>
  );
}
