"use client";

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

const config: ReportConfig = {
  key: "production_report_v2",
  title: "생산 리포트",
  description: "생산 실적 다중 조건 비교 분석",
  apiEndpoint: "/report/production/data",
  metrics: [
    { id: "prodQty", name: "생산량", unit: "EA", color: "#3b82f6" },
    { id: "planQty", name: "계획수량", unit: "EA", color: "#10b981" },
    { id: "defectQty", name: "불량수량", unit: "EA", color: "#ef4444" },
    { id: "defectRate", name: "불량률", unit: "%", color: "#f59e0b", isRate: true },
    { id: "opRate", name: "가동률", unit: "%", color: "#8b5cf6", isRate: true },
    { id: "achRate", name: "달성률", unit: "%", color: "#ec4899", isRate: true },
    { id: "runTime", name: "가동시간", unit: "H", color: "#06b6d4" },
    { id: "downTime", name: "비가동시간", unit: "H", color: "#84cc16" },
  ],
  groupByOptions: [
    { id: "process", name: "공정별" },
    { id: "equipment", name: "설비별" },
    { id: "item", name: "품목별" },
    { id: "worker", name: "작업자별" },
    { id: "monthly", name: "월별" },
    { id: "quarterly", name: "분기별" },
    { id: "weekly", name: "주별" },
    { id: "daily", name: "일별" },
  ],
  defaultGroupBy: "process",
  defaultMetrics: ["prodQty"],
  thresholds: [
    { id: "defect", label: "불량률 ≥", defaultValue: 3, unit: "%" },
    { id: "opRate", label: "가동률 ≤", defaultValue: 85, unit: "%" },
  ],
  filterFieldDefs: [
    { id: "process", name: "공정", type: "select", optionKey: "processes" },
    { id: "equipment", name: "설비", type: "select", optionKey: "equipment" },
    { id: "item", name: "품목", type: "select", optionKey: "items" },
    { id: "worker", name: "작업자", type: "select", optionKey: "workers" },
    { id: "prodQty", name: "생산량", type: "number" },
    { id: "defectRate", name: "불량률", type: "number" },
  ],
  drilldownColumns: [
    { id: "date", name: "날짜", format: "date" },
    { id: "process", name: "공정" },
    { id: "equipment", name: "설비" },
    { id: "item", name: "품목" },
    { id: "worker", name: "작업자" },
    { id: "prodQty", name: "생산량", align: "right", format: "number" },
    { id: "planQty", name: "계획수량", align: "right", format: "number" },
    { id: "defectQty", name: "불량수량", align: "right", format: "number" },
    { id: "defectRate", name: "불량률(%)", align: "right", format: "number" },
  ],
  rawDataColumns: [
    { id: "date", name: "날짜", format: "date" },
    { id: "process", name: "공정" },
    { id: "equipment", name: "설비" },
    { id: "item", name: "품목" },
    { id: "worker", name: "작업자" },
    { id: "prodQty", name: "생산량", align: "right", format: "number" },
    { id: "planQty", name: "계획수량", align: "right", format: "number" },
    { id: "defectQty", name: "불량수량", align: "right", format: "number" },
    { id: "runTime", name: "가동시간", align: "right", format: "number" },
    { id: "downTime", name: "비가동시간", align: "right", format: "number" },
  ],
  enrichRow: (d) => ({
    ...d,
    defectRate: d.prodQty > 0 ? parseFloat((d.defectQty / d.prodQty * 100).toFixed(1)) : 0,
    opRate: (d.runTime + d.downTime) > 0
      ? parseFloat(((d.runTime / (d.runTime + d.downTime)) * 100).toFixed(1))
      : 0,
    achRate: d.planQty > 0 ? parseFloat((d.prodQty / d.planQty * 100).toFixed(1)) : 0,
  }),
  emptyMessage: "생산 데이터가 없습니다",
};

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