"use client";

import { useMemo } from "react";
import ReportEngine, { ReportConfig } from "@/components/admin/report/ReportEngine";
import { useAuth } from "@/hooks/useAuth";

const GLASS_COMPANIES = new Set([]);

function buildConfig(isGlass: boolean): ReportConfig {
  const baseMetrics = [
    { id: "orderAmt", name: "수주금액", unit: "원", color: "#3b82f6" },
    { id: "orderQty", name: "수주수량", unit: "EA", color: "#10b981" },
    { id: "shipQty", name: "출하수량", unit: "EA", color: "#ef4444" },
    { id: "unitPrice", name: "단가", unit: "원", color: "#8b5cf6" },
    { id: "orderCount", name: "수주건수", unit: "건", color: "#f59e0b" },
  ];
  const glassMetrics = [
    { id: "totalArea", name: "총 면적", unit: "㎡", color: "#0891b2" },
    { id: "width", name: "가로", unit: "mm", color: "#14b8a6" },
    { id: "height", name: "세로", unit: "mm", color: "#d946ef" },
    { id: "thickness", name: "두께", unit: "mm", color: "#f97316" },
    { id: "areaSingle", name: "면적(건당)", unit: "㎡", color: "#06b6d4" },
  ];

  const baseGroupByOptions = [
    { id: "customer", name: "거래처별" },
    { id: "item", name: "품목별" },
    { id: "status", name: "상태별" },
    { id: "monthly", name: "월별" },
    { id: "quarterly", name: "분기별" },
    { id: "weekly", name: "주별" },
    { id: "daily", name: "일별" },
  ];
  const glassGroupByOptions = [
    { id: "thickness", name: "두께별" },
    { id: "size", name: "사이즈별 (가로×세로)" },
    { id: "sizeRange", name: "사이즈 구간별" },
  ];

  const baseGroupableFields = [
    { id: "customer", name: "거래처" },
    { id: "item", name: "품목" },
    { id: "status", name: "상태" },
    { id: "monthly", name: "월" },
    { id: "quarterly", name: "분기" },
    { id: "weekly", name: "주" },
    { id: "daily", name: "일" },
  ];
  const glassGroupableFields = [
    { id: "thickness", name: "두께" },
    { id: "size", name: "사이즈" },
    { id: "sizeRange", name: "사이즈 구간" },
  ];

  const baseFilterFieldDefs = [
    { id: "customer", name: "거래처", type: "select" as const, optionKey: "customers" },
    { id: "item", name: "품목", type: "select" as const, optionKey: "items" },
    { id: "status", name: "상태", type: "select" as const, optionKey: "statuses" },
    { id: "orderAmt", name: "수주금액", type: "number" as const },
    { id: "orderQty", name: "수주수량", type: "number" as const },
  ];
  const glassFilterFieldDefs = [
    { id: "thickness", name: "두께", type: "number" as const },
    { id: "width", name: "가로", type: "number" as const },
    { id: "height", name: "세로", type: "number" as const },
    { id: "totalArea", name: "총 면적", type: "number" as const },
  ];

  const baseDrilldownColumns = [
    { id: "date", name: "날짜", format: "date" as const },
    { id: "order_no", name: "수주번호" },
    { id: "customer", name: "거래처" },
    { id: "item", name: "품목" },
    { id: "status", name: "상태", format: "badge" as const },
    { id: "orderQty", name: "수주수량", align: "right" as const, format: "number" as const },
    { id: "unitPrice", name: "단가", align: "right" as const, format: "number" as const },
    { id: "orderAmt", name: "수주금액", align: "right" as const, format: "number" as const },
    { id: "shipQty", name: "출하수량", align: "right" as const, format: "number" as const },
  ];
  const glassDrilldownColumns = [
    { id: "width", name: "가로", align: "right" as const },
    { id: "height", name: "세로", align: "right" as const },
    { id: "thickness", name: "두께", align: "right" as const },
    { id: "areaSingle", name: "면적(㎡)", align: "right" as const, format: "number" as const },
    { id: "totalArea", name: "총면적(㎡)", align: "right" as const, format: "number" as const },
  ];

  const baseRawDataColumns = [
    { id: "date", name: "날짜", format: "date" as const },
    { id: "order_no", name: "수주번호" },
    { id: "customer", name: "거래처" },
    { id: "part_code", name: "품목코드" },
    { id: "item", name: "품목명" },
    { id: "status", name: "상태", format: "badge" as const },
    { id: "orderQty", name: "수주수량", align: "right" as const, format: "number" as const },
    { id: "unitPrice", name: "단가", align: "right" as const, format: "number" as const },
    { id: "orderAmt", name: "수주금액", align: "right" as const, format: "number" as const },
    { id: "shipQty", name: "출하수량", align: "right" as const, format: "number" as const },
  ];

  return {
    key: "sales_report_v2",
    title: "영업 리포트",
    description: "다중 조건 비교 분석",
    apiEndpoint: "/sales-report/data",
    metrics: isGlass ? [...baseMetrics, ...glassMetrics] : baseMetrics,
    groupByOptions: isGlass ? [...baseGroupByOptions, ...glassGroupByOptions] : baseGroupByOptions,
    groupableFields: isGlass ? [...baseGroupableFields, ...glassGroupableFields] : baseGroupableFields,
    defaultGroupBy: "customer",
    defaultMetrics: ["orderAmt"],
    thresholds: [
      { id: "low", label: "목표 미달 ≤", defaultValue: 80, unit: "%" },
      { id: "high", label: "목표 초과 ≥", defaultValue: 120, unit: "%" },
    ],
    filterFieldDefs: isGlass ? [...baseFilterFieldDefs, ...glassFilterFieldDefs] : baseFilterFieldDefs,
    drilldownColumns: isGlass
      ? [...baseDrilldownColumns.slice(0, 4), ...glassDrilldownColumns.slice(0, 4), ...baseDrilldownColumns.slice(4), glassDrilldownColumns[4]]
      : baseDrilldownColumns,
    rawDataColumns: isGlass
      ? [...baseRawDataColumns.slice(0, 5), ...glassDrilldownColumns.slice(0, 4), ...baseRawDataColumns.slice(5), glassDrilldownColumns[4]]
      : baseRawDataColumns,
    emptyMessage: "수주 데이터가 없습니다",
  };
}

export default function SalesReportPage() {
  const { user } = useAuth();
  const companyCode = user?.companyCode || "";
  const config = useMemo(() => buildConfig(GLASS_COMPANIES.has(companyCode)), [companyCode]);
  return <ReportEngine config={config} />;
}
