"use client";

import ReportEngine, { ReportConfig } from "@/components/admin/report/ReportEngine";

const config: ReportConfig = {
  key: "purchase_report_v2",
  title: "구매 리포트",
  description: "구매/발주 다중 조건 비교 분석",
  apiEndpoint: "/report/purchase/data",
  metrics: [
    { id: "orderAmt", name: "발주금액", unit: "원", color: "#3b82f6" },
    { id: "receiveAmt", name: "입고금액", unit: "원", color: "#10b981" },
    { id: "orderQty", name: "발주수량", unit: "EA", color: "#f59e0b" },
    { id: "receiveQty", name: "입고수량", unit: "EA", color: "#8b5cf6" },
    { id: "receiveRate", name: "입고율", unit: "%", color: "#ef4444", isRate: true },
    { id: "unitPrice", name: "단가", unit: "원", color: "#ec4899" },
    { id: "orderCnt", name: "발주건수", unit: "건", color: "#f97316" },
  ],
  groupByOptions: [
    { id: "supplier", name: "공급업체별" },
    { id: "item", name: "품목별" },
    { id: "manager", name: "구매담당별" },
    { id: "status", name: "상태별" },
    { id: "monthly", name: "월별" },
    { id: "quarterly", name: "분기별" },
    { id: "weekly", name: "주별" },
    { id: "daily", name: "일별" },
  ],
  defaultGroupBy: "supplier",
  defaultMetrics: ["orderAmt"],
  thresholds: [
    { id: "delay", label: "납기지연 ≥", defaultValue: 3, unit: "일" },
    { id: "price", label: "단가변동률 ≥", defaultValue: 10, unit: "%" },
  ],
  filterFieldDefs: [
    { id: "supplier", name: "공급업체", type: "select", optionKey: "suppliers" },
    { id: "item", name: "품목", type: "select", optionKey: "items" },
    { id: "manager", name: "구매담당", type: "select", optionKey: "managers" },
    { id: "status", name: "상태", type: "select", optionKey: "statuses" },
    { id: "orderAmt", name: "발주금액", type: "number" },
    { id: "orderQty", name: "발주수량", type: "number" },
  ],
  drilldownColumns: [
    { id: "date", name: "날짜", format: "date" },
    { id: "purchase_no", name: "발주번호" },
    { id: "supplier", name: "공급업체" },
    { id: "item", name: "품목" },
    { id: "status", name: "상태", format: "badge" },
    { id: "orderQty", name: "발주수량", align: "right", format: "number" },
    { id: "receiveQty", name: "입고수량", align: "right", format: "number" },
    { id: "unitPrice", name: "단가", align: "right", format: "number" },
    { id: "orderAmt", name: "발주금액", align: "right", format: "number" },
  ],
  rawDataColumns: [
    { id: "date", name: "날짜", format: "date" },
    { id: "purchase_no", name: "발주번호" },
    { id: "supplier", name: "공급업체" },
    { id: "item_code", name: "품목코드" },
    { id: "item", name: "품목명" },
    { id: "status", name: "상태", format: "badge" },
    { id: "orderQty", name: "발주수량", align: "right", format: "number" },
    { id: "receiveQty", name: "입고수량", align: "right", format: "number" },
    { id: "unitPrice", name: "단가", align: "right", format: "number" },
    { id: "orderAmt", name: "발주금액", align: "right", format: "number" },
    { id: "manager", name: "담당자" },
  ],
  enrichRow: (d) => ({
    ...d,
    receiveRate: d.orderQty > 0 ? parseFloat((d.receiveQty / d.orderQty * 100).toFixed(1)) : 0,
  }),
  emptyMessage: "구매 데이터가 없습니다",
};

export default function PurchaseReportPage() {
  return <ReportEngine config={config} />;
}
