"use client";

import React, { useCallback, useEffect, useMemo, useState } from "react";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import {
  Select, SelectContent, SelectItem, SelectTrigger, SelectValue,
} from "@/components/ui/select";
import {
  Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle,
} from "@/components/ui/dialog";
import { Save, Loader2, Search, Truck } from "lucide-react";
import { toast } from "sonner";
import { useAuth } from "@/hooks/useAuth";
import { DataGrid, DataGridColumn } from "@/components/common/DataGrid";
import { CustomerSelect } from "@/components/common/CustomerSelect";
import { PartSelect } from "@/components/common/PartSelect";
import { CommCodeSelect } from "@/components/common/CommCodeSelect";
import { SmartSelect, SmartSelectOption } from "@/components/common/SmartSelect";
import { PageHeader } from "@/components/common/PageHeader";
import { CompactFilterBar, CompactFilterField, CompactDateRange } from "@/components/common/CompactFilterBar";
import { ProjectInfoDialog, ProjectInfoData } from "@/components/project/ProjectInfoDialog";
import { salesSaleApi, SaleListRow, SaleRegisterBody } from "@/lib/api/salesSale";
import { exportToExcel } from "@/lib/utils/excelExport";

// SaleListRow → 정규화된 ProjectInfoData (wace 운영판 다이얼로그 1:1)
const toProjectInfo = (r: SaleListRow): ProjectInfoData => ({
  orderType: r.order_type_name,
  productType: r.product_type_name,
  area: r.nation_name,
  customer: r.customer,
  paidType: r.payment_type_name,
  regDate: r.receipt_date,
  currency: r.contract_currency_name,
  exchangeRate: r.exchange_rate,
  partNo: r.product_no,
  partName: r.product_name,
  serialNo: r.serial_no,
  reqDelDate: r.request_date,
  customerRequest: r.customer_request,
  returnReason: r.return_reason_name,
});

// wace_plm salesMgmtList.jsp 컬럼 순서/라벨에 맞춤
const GRID_COLUMNS: DataGridColumn[] = [
  { key: "project_no", label: "프로젝트번호", width: "w-[170px]", frozen: true },
  { key: "order_type_name", label: "주문유형", width: "w-[115px]", align: "center" },
  { key: "order_date", label: "발주일", width: "w-[115px]", align: "center" },
  { key: "po_no", label: "발주번호", width: "w-[140px]" },
  { key: "request_date", label: "요청납기", width: "w-[115px]", align: "center" },
  { key: "shipping_date", label: "출하일", width: "w-[115px]", align: "center" },
  { key: "customer", label: "고객사", width: "w-[160px]" },
  { key: "product_name", label: "품명", width: "w-[180px]" },
  { key: "order_quantity", label: "수주수량", width: "w-[115px]", align: "right", formatNumber: true },
  { key: "sales_quantity", label: "판매수량", width: "w-[115px]", align: "right", formatNumber: true },
  { key: "remaining_quantity", label: "잔량", width: "w-[95px]", align: "right", formatNumber: true },
  { key: "sales_unit_price", label: "판매단가", width: "w-[115px]", formatMoney: true },
  { key: "sales_supply_price", label: "판매공급가액", width: "w-[140px]", formatMoney: true },
  { key: "sales_vat", label: "부가세", width: "w-[115px]", formatMoney: true },
  { key: "sales_total_amount", label: "판매총액", width: "w-[130px]", formatMoney: true },
  { key: "sales_total_amount_krw", label: "판매원화총액", width: "w-[140px]", formatMoney: true },
  { key: "remaining_amount_krw", label: "잔량원화총액", width: "w-[140px]", formatMoney: true },
  { key: "order_status_name", label: "수주상태", width: "w-[115px]", align: "center" },
  { key: "sales_status", label: "판매상태", width: "w-[115px]", align: "center" },
  { key: "production_status", label: "생산상태", width: "w-[115px]", align: "center" },
  { key: "shipping_order_status", label: "출하지시상태", width: "w-[135px]", align: "center" },
  { key: "payment_type_name", label: "유/무상", width: "w-[100px]", align: "center" },
  { key: "sales_currency_name", label: "환종", width: "w-[95px]", align: "center" },
  { key: "sales_exchange_rate", label: "환율", width: "w-[95px]", formatMoney: true },
  { key: "serial_no", label: "S/N", width: "w-[140px]" },
  { key: "split_serial_no", label: "분할S/N", width: "w-[140px]" },
  { key: "product_no", label: "품번", width: "w-[120px]" },
  /* wace salesMgmtList.jsp 503~519 — 비활성(주석) 컬럼. 활성화 시 아래 주석 해제.
  { key: "product_type_name", label: "제품구분", width: "w-[90px]", align: "center" },
  { key: "nation_name", label: "국내/해외", width: "w-[90px]", align: "center" },
  { key: "receipt_date", label: "접수일", width: "w-[115px]", align: "center" },
  { key: "customer_request", label: "고객사요청사항", width: "w-[180px]" },
  { key: "cu01_cnt", label: "주문서첨부", width: "w-[90px]", align: "center", renderType: "clip" },
  { key: "shipping_method", label: "출하방법", width: "w-[90px]", align: "center" },
  { key: "manager_name", label: "담당자", width: "w-[100px]", align: "center" },
  { key: "incoterms", label: "인도조건", width: "w-[90px]", align: "center" },
  */
  { key: "has_transaction_statement", label: "거래명세서", width: "w-[115px]", align: "center" },
];

export default function SalesSalePage() {
  const { user } = useAuth();

  const [rows, setRows] = useState<any[]>([]);
  const [loading, setLoading] = useState(false);
  const [selected, setSelected] = useState<SaleListRow | null>(null);
  // wace salesMgmtList.jsp 검색 폼: 1줄 7개 / 2줄 3개
  const [searchForm, setSearchForm] = useState({
    orderType: "", poNo: "", customer_objid: "", search_partObjId: "",
    serialNo: "", shippingStatus: "", salesStatus: "",
    orderDateFrom: "", orderDateTo: "",
    shippingDateFrom: "", shippingDateTo: "",
  });

  // 프로젝트번호 셀 클릭 → 프로젝트 상세 정보 다이얼로그 (wace 운영판 1:1)
  const [infoOpen, setInfoOpen] = useState(false);
  const [infoData, setInfoData] = useState<ProjectInfoData | null>(null);
  const columns = useMemo(
    () => GRID_COLUMNS.map((col) =>
      col.key === "project_no"
        ? { ...col, onClick: (row: any) => { setInfoData(toProjectInfo(row as SaleListRow)); setInfoOpen(true); } }
        : col,
    ),
    [],
  );

  const [registerOpen, setRegisterOpen] = useState(false);
  const [saving, setSaving] = useState(false);
  const [form, setForm] = useState<SaleRegisterBody>({
    project_no: "", sales_currency: "KRW", sales_exchange_rate: 1,
  });

  const fetchList = useCallback(async () => {
    if (!user) return;
    setLoading(true);
    try {
      const params: Record<string, string> = {};
      Object.entries(searchForm).forEach(([k, v]) => { if (v) params[k] = v; });
      const data = await salesSaleApi.saleList(params);
      const mapped = data.map((r, i) => ({ ...r, id: `${r.project_no}-${r.contract_item_objid}-${i}` }));
      setRows(mapped);
    } catch { toast.error("판매 목록 조회 실패"); }
    finally { setLoading(false); }
  }, [user, searchForm]);

  useEffect(() => { fetchList(); }, [fetchList]);

  // ─── 하단 통계 ──────────────────────────────────────────────
  // 판매 라인 수 / 수주·판매·잔량 수량 합계 / 판매공급가액·판매원화총액·잔량원화총액 합계
  const saleSummary = useMemo(() => {
    const count = rows.length;
    const ordQty   = rows.reduce((acc, r) => acc + Number(r.order_quantity || 0), 0);
    const salQty   = rows.reduce((acc, r) => acc + Number(r.sales_quantity || 0), 0);
    const remQty   = rows.reduce((acc, r) => acc + Number(r.remaining_quantity || 0), 0);
    const supplySum = rows.reduce((acc, r) => acc + Number(r.sales_supply_price || 0), 0);
    const totalKrw  = rows.reduce((acc, r) => acc + Number(r.sales_total_amount_krw || 0), 0);
    const remKrw    = rows.reduce((acc, r) => acc + Number(r.remaining_amount_krw || 0), 0);
    const money = (n: number) => n.toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2 });
    const intFmt = (n: number) => n.toLocaleString();
    return [
      { label: "판매 라인", value: intFmt(count), suffix: "건" },
      { label: "수주수량 합계", value: intFmt(ordQty) },
      { label: "판매수량 합계", value: intFmt(salQty) },
      { label: "잔량 합계", value: intFmt(remQty) },
      { label: "판매공급가액 합계", value: money(supplySum) },
      { label: "판매원화총액 합계", value: money(totalKrw) },
      { label: "잔량원화총액 합계", value: money(remKrw) },
    ];
  }, [rows]);

  const openRegister = () => {
    if (!selected) { toast.warning("판매등록할 행을 선택하세요."); return; }
    setForm({
      project_no: selected.project_no,
      shipping_order_status: selected.shipping_order_status ?? "PENDING",
      serial_no: selected.serial_no ?? "",
      sales_quantity: Number(selected.sales_quantity ?? selected.order_quantity ?? 0),
      sales_unit_price: Number(selected.sales_unit_price ?? 0),
      sales_supply_price: Number(selected.sales_supply_price ?? 0),
      sales_vat: Number(selected.sales_vat ?? 0),
      sales_total_amount: Number(selected.sales_total_amount ?? 0),
      sales_currency: selected.sales_currency ?? "KRW",
      sales_exchange_rate: Number(selected.sales_exchange_rate ?? 1),
      shipping_date: selected.shipping_date ?? "",
      shipping_method: selected.shipping_method ?? "",
      manager_user_id: selected.manager_user_id ?? "",
      incoterms: selected.incoterms ?? "",
      has_split_shipment: selected.has_split_shipment ?? false,
    });
    setRegisterOpen(true);
  };

  const handleRegister = async () => {
    setSaving(true);
    try {
      // 자동 계산
      const qty = Number(form.sales_quantity ?? 0);
      const price = Number(form.sales_unit_price ?? 0);
      const supply = form.sales_supply_price && form.sales_supply_price > 0 ? form.sales_supply_price : qty * price;
      const vat = form.sales_vat && form.sales_vat > 0 ? form.sales_vat : Math.round(supply * 0.1 * 100) / 100;
      const total = form.sales_total_amount && form.sales_total_amount > 0 ? form.sales_total_amount : supply + vat;
      await salesSaleApi.registerSale({ ...form, sales_supply_price: supply, sales_vat: vat, sales_total_amount: total });
      toast.success("판매가 등록되었습니다.");
      setRegisterOpen(false);
      await fetchList();
    } catch (err: any) {
      toast.error(`저장 실패: ${err?.response?.data?.message ?? err.message}`);
    } finally { setSaving(false); }
  };

  const shippingStatusOpts: SmartSelectOption[] = [
    { code: "PENDING",   label: "대기" },
    { code: "COMPLETED", label: "완료" },
    { code: "CANCELLED", label: "취소" },
  ];

  const handleReset = () => setSearchForm({
    orderType: "", poNo: "", customer_objid: "", search_partObjId: "",
    serialNo: "", shippingStatus: "", salesStatus: "",
    orderDateFrom: "", orderDateTo: "",
    shippingDateFrom: "", shippingDateTo: "",
  });

  return (
    <div className="flex h-full flex-col overflow-hidden p-2 gap-2">
      <PageHeader
        loading={loading}
        onSearch={fetchList}
        onReset={handleReset}
        actions={
        <Button size="sm" className="h-8 gap-1 bg-emerald-600 hover:bg-emerald-700 text-white text-xs" onClick={openRegister} disabled={!selected}>
          <Truck className="h-3.5 w-3.5" />출하지시/판매등록
        </Button>
      } />

      <CompactFilterBar totalText={<>총 {rows.length.toLocaleString()}건 (라인 단위)</>}>
        <CompactFilterField label="주문유형" width={130}>
          <CommCodeSelect groupId="0000167"
            value={searchForm.orderType}
            onValueChange={(v) => setSearchForm({ ...searchForm, orderType: v })} />
        </CompactFilterField>
        <CompactFilterField label="발주번호" width={140}>
          <Input placeholder="발주번호 검색"
                 value={searchForm.poNo}
                 onChange={(e) => setSearchForm({ ...searchForm, poNo: e.target.value })} />
        </CompactFilterField>
        <CompactFilterField label="고객사" width={160}>
          <CustomerSelect
            value={searchForm.customer_objid}
            onValueChange={(v) => setSearchForm({ ...searchForm, customer_objid: v })} />
        </CompactFilterField>
        <CompactFilterField label="품번" width={130}>
          <PartSelect mode="partNo"
            value={searchForm.search_partObjId}
            onValueChange={(v) => setSearchForm({ ...searchForm, search_partObjId: v })} />
        </CompactFilterField>
        <CompactFilterField label="품명" width={150}>
          <PartSelect mode="partName"
            value={searchForm.search_partObjId}
            onValueChange={(v) => setSearchForm({ ...searchForm, search_partObjId: v })} />
        </CompactFilterField>
        <CompactFilterField label="S/N" width={120}>
          <Input value={searchForm.serialNo}
                 onChange={(e) => setSearchForm({ ...searchForm, serialNo: e.target.value })} />
        </CompactFilterField>
        <CompactFilterField label="출하지시상태" width={130}>
          <SmartSelect
            options={shippingStatusOpts}
            value={searchForm.shippingStatus}
            onValueChange={(v) => setSearchForm({ ...searchForm, shippingStatus: v })}
            placeholder="전체"
          />
        </CompactFilterField>
        <CompactFilterField label="판매상태" width={130}>
          <CommCodeSelect groupId="0900207"
            value={searchForm.salesStatus}
            onValueChange={(v) => setSearchForm({ ...searchForm, salesStatus: v })} />
        </CompactFilterField>
        <CompactFilterField label="발주일" width={280}>
          <CompactDateRange
            from={searchForm.orderDateFrom}
            setFrom={(v) => setSearchForm({ ...searchForm, orderDateFrom: v })}
            to={searchForm.orderDateTo}
            setTo={(v) => setSearchForm({ ...searchForm, orderDateTo: v })}
          />
        </CompactFilterField>
        <CompactFilterField label="출하일" width={280}>
          <CompactDateRange
            from={searchForm.shippingDateFrom}
            setFrom={(v) => setSearchForm({ ...searchForm, shippingDateFrom: v })}
            to={searchForm.shippingDateTo}
            setTo={(v) => setSearchForm({ ...searchForm, shippingDateTo: v })}
          />
        </CompactFilterField>
      </CompactFilterBar>

      <DataGrid
        gridId="sales-sale"
        columns={columns}
        data={rows}
        selectedId={selected ? `${selected.project_no}-${selected.contract_item_objid}-0` : null}
        onSelect={(id) => setSelected(id ? rows.find((r) => r.id === id) ?? null : null)}
        onRowDoubleClick={(row) => { setSelected(row); openRegister(); }}
        emptyMessage={loading ? "조회 중..." : "데이터가 없습니다"}
        loading={loading}
        showColumnSettings
        paginationStyle="range"
        pageSizeOptions={[10, 15, 20, 50, 100]}
        summaryStats={saleSummary}
        onRefresh={fetchList}
        onDownload={() => {
          if (rows.length === 0) { toast.info("내보낼 데이터가 없습니다."); return; }
          const exportRows = rows.map((r) => {
            const out: Record<string, any> = {};
            GRID_COLUMNS.forEach((col) => { out[col.label] = r[col.key] ?? ""; });
            return out;
          });
          exportToExcel(exportRows, "판매관리.xlsx", "판매관리");
        }}
        showChart
      />

      <ProjectInfoDialog open={infoOpen} onOpenChange={setInfoOpen} data={infoData} />

      {/* 출하지시/판매등록 Dialog */}
      <Dialog open={registerOpen} onOpenChange={setRegisterOpen}>
        <DialogContent className="max-w-3xl">
          <DialogHeader>
            <DialogTitle>출하지시 / 판매 등록</DialogTitle>
            <DialogDescription>{selected?.contract_no} · {selected?.product_name}</DialogDescription>
          </DialogHeader>

          <div className="grid grid-cols-3 gap-3">
            <div><Label className="text-xs">판매수량</Label>
              <Input type="number" value={form.sales_quantity ?? 0}
                     onChange={(e) => setForm({ ...form, sales_quantity: Number(e.target.value) })} /></div>
            <div><Label className="text-xs">판매단가</Label>
              <Input type="number" step="0.01" value={form.sales_unit_price ?? 0}
                     onChange={(e) => setForm({ ...form, sales_unit_price: Number(e.target.value) })} /></div>
            <div><Label className="text-xs">통화</Label>
              <Input value={form.sales_currency ?? "KRW"} onChange={(e) => setForm({ ...form, sales_currency: e.target.value })} /></div>
            <div><Label className="text-xs">환율</Label>
              <Input type="number" step="0.0001" value={form.sales_exchange_rate ?? 1}
                     onChange={(e) => setForm({ ...form, sales_exchange_rate: Number(e.target.value) })} /></div>
            <div><Label className="text-xs">공급가액 (자동)</Label>
              <Input type="number" step="0.01" value={form.sales_supply_price ?? 0}
                     onChange={(e) => setForm({ ...form, sales_supply_price: Number(e.target.value) })} /></div>
            <div><Label className="text-xs">부가세 (자동 10%)</Label>
              <Input type="number" step="0.01" value={form.sales_vat ?? 0}
                     onChange={(e) => setForm({ ...form, sales_vat: Number(e.target.value) })} /></div>
            <div><Label className="text-xs">총액 (자동)</Label>
              <Input type="number" step="0.01" value={form.sales_total_amount ?? 0}
                     onChange={(e) => setForm({ ...form, sales_total_amount: Number(e.target.value) })} /></div>
            <div><Label className="text-xs">출하일</Label>
              <Input type="date" value={form.shipping_date ?? ""} onChange={(e) => setForm({ ...form, shipping_date: e.target.value })} /></div>
            <div><Label className="text-xs">출하방법</Label>
              <Select value={form.shipping_method ?? ""} onValueChange={(v) => setForm({ ...form, shipping_method: v })}>
                <SelectTrigger><SelectValue placeholder="선택" /></SelectTrigger>
                <SelectContent>
                  <SelectItem value="DIRECT">직배송</SelectItem>
                  <SelectItem value="PARCEL">택배</SelectItem>
                </SelectContent>
              </Select></div>
            <div><Label className="text-xs">출하지시 상태</Label>
              <Select value={form.shipping_order_status ?? "PENDING"} onValueChange={(v) => setForm({ ...form, shipping_order_status: v })}>
                <SelectTrigger><SelectValue /></SelectTrigger>
                <SelectContent>
                  <SelectItem value="PENDING">대기</SelectItem>
                  <SelectItem value="COMPLETED">완료</SelectItem>
                  <SelectItem value="CANCELLED">취소</SelectItem>
                </SelectContent>
              </Select></div>
            <div><Label className="text-xs">담당자 ID</Label>
              <Input value={form.manager_user_id ?? ""} onChange={(e) => setForm({ ...form, manager_user_id: e.target.value })} /></div>
            <div><Label className="text-xs">인도조건</Label>
              <Input value={form.incoterms ?? ""} onChange={(e) => setForm({ ...form, incoterms: e.target.value })} placeholder="EXW/FOB/CIF" /></div>
            <div className="col-span-3"><Label className="text-xs">시리얼 번호 (콤마 구분)</Label>
              <Input value={form.serial_no ?? ""} onChange={(e) => setForm({ ...form, serial_no: e.target.value })} /></div>
          </div>

          <DialogFooter>
            <Button variant="outline" onClick={() => setRegisterOpen(false)} disabled={saving}>취소</Button>
            <Button onClick={handleRegister} disabled={saving}>
              {saving ? <Loader2 className="w-4 h-4 mr-1 animate-spin" /> : <Save className="w-4 h-4 mr-1" />}저장
            </Button>
          </DialogFooter>
        </DialogContent>
      </Dialog>
    </div>
  );
}
