"use client";

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

const config: ReportConfig = {
  key: "quality_report_v2",
  title: "품질 리포트",
  description: "품질/검사 다중 조건 비교 분석",
  apiEndpoint: "/report/quality/data",
  metrics: [
    { id: "defectQty", name: "불량수량", unit: "EA", color: "#ef4444" },
    { id: "defectRate", name: "불량률", unit: "%", color: "#f59e0b", isRate: true },
    { id: "inspQty", name: "검사수량", unit: "EA", color: "#3b82f6" },
    { id: "passQty", name: "합격수량", unit: "EA", color: "#10b981" },
    { id: "passRate", name: "합격률", unit: "%", color: "#8b5cf6", isRate: true },
    { id: "reworkQty", name: "재작업수량", unit: "EA", color: "#ec4899" },
    { id: "scrapQty", name: "폐기수량", unit: "EA", color: "#06b6d4" },
    { id: "claimCnt", name: "클레임건수", unit: "건", color: "#84cc16" },
  ],
  groupByOptions: [
    { id: "item", name: "품목별" },
    { id: "defectType", name: "불량유형별" },
    { id: "process", name: "공정별" },
    { id: "inspector", name: "검사자별" },
    { id: "monthly", name: "월별" },
    { id: "quarterly", name: "분기별" },
    { id: "weekly", name: "주별" },
    { id: "daily", name: "일별" },
  ],
  defaultGroupBy: "item",
  defaultMetrics: ["defectQty"],
  thresholds: [
    { id: "defectRate", label: "불량률 ≥", defaultValue: 5, unit: "%" },
    { id: "defectQty", label: "불량수량 ≥", defaultValue: 20, unit: "EA" },
  ],
  filterFieldDefs: [
    { id: "item", name: "품목", type: "select", optionKey: "items" },
    { id: "defectType", name: "불량유형", type: "select", optionKey: "defectTypes" },
    { id: "process", name: "공정", type: "select", optionKey: "processes" },
    { id: "inspector", name: "검사자", type: "select", optionKey: "inspectors" },
    { id: "defectQty", name: "불량수량", type: "number" },
    { id: "defectRate", name: "불량률", type: "number" },
  ],
  drilldownColumns: [
    { id: "date", name: "날짜", format: "date" },
    { id: "item", name: "품목" },
    { id: "defectType", name: "불량유형" },
    { id: "process", name: "공정" },
    { id: "inspector", name: "검사자" },
    { id: "inspQty", name: "검사수량", align: "right", format: "number" },
    { id: "defectQty", name: "불량수량", align: "right", format: "number" },
    { id: "defectRate", name: "불량률(%)", align: "right", format: "number" },
    { id: "passRate", name: "합격률(%)", align: "right", format: "number" },
  ],
  rawDataColumns: [
    { id: "date", name: "날짜", format: "date" },
    { id: "item", name: "품목" },
    { id: "defectType", name: "불량유형" },
    { id: "process", name: "공정" },
    { id: "inspector", name: "검사자" },
    { id: "inspQty", name: "검사수량", align: "right", format: "number" },
    { id: "passQty", name: "합격수량", align: "right", format: "number" },
    { id: "defectQty", name: "불량수량", align: "right", format: "number" },
    { id: "reworkQty", name: "재작업", align: "right", format: "number" },
    { id: "scrapQty", name: "폐기", align: "right", format: "number" },
  ],
  enrichRow: (d) => ({
    ...d,
    defectRate: d.inspQty > 0 ? parseFloat((d.defectQty / d.inspQty * 100).toFixed(1)) : 0,
    passRate: d.inspQty > 0 ? parseFloat((d.passQty / d.inspQty * 100).toFixed(1)) : 0,
  }),
  emptyMessage: "품질 데이터가 없습니다",
};

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