"use client";

import React, { useMemo } from "react";
import { NumberingRuleConfig } from "@/types/numbering-rule";

interface NumberingRulePreviewProps {
  config: NumberingRuleConfig;
  compact?: boolean;
}

export const NumberingRulePreview: React.FC<NumberingRulePreviewProps> = ({ config, compact = false }) => {
  const generatedCode = useMemo(() => {
    if (!config.parts || config.parts.length === 0) {
      return "규칙을 추가해주세요";
    }

    const parts = config.parts
      .sort((a, b) => a.order - b.order)
      .map((part) => {
        if (part.generationMethod === "manual") {
          return part.manualConfig?.value || "XXX";
        }

        const autoConfig = part.autoConfig || {};

        switch (part.partType) {
          // 1. 순번 (자동 증가)
          case "sequence": {
            const length = autoConfig.sequenceLength || 3;
            const startFrom = autoConfig.startFrom || 1;
            return String(startFrom).padStart(length, "0");
          }

          // 2. 숫자 (고정 자릿수)
          case "number": {
            const length = autoConfig.numberLength || 4;
            const value = autoConfig.numberValue || 0;
            return String(value).padStart(length, "0");
          }

          // 3. 날짜
          case "date": {
            const format = autoConfig.dateFormat || "YYYYMMDD";

            // 컬럼 기준 생성인 경우 placeholder 표시
            if (autoConfig.useColumnValue && autoConfig.sourceColumnName) {
              // 형식에 맞는 placeholder 반환
              switch (format) {
                case "YYYY":
                  return "[YYYY]";
                case "YY":
                  return "[YY]";
                case "YYYYMM":
                  return "[YYYYMM]";
                case "YYMM":
                  return "[YYMM]";
                case "YYYYMMDD":
                  return "[YYYYMMDD]";
                case "YYMMDD":
                  return "[YYMMDD]";
                default:
                  return "[DATE]";
              }
            }

            // 현재 날짜 기준 생성
            const now = new Date();
            const year = now.getFullYear();
            const month = String(now.getMonth() + 1).padStart(2, "0");
            const day = String(now.getDate()).padStart(2, "0");

            switch (format) {
              case "YYYY":
                return String(year);
              case "YY":
                return String(year).slice(-2);
              case "YYYYMM":
                return `${year}${month}`;
              case "YYMM":
                return `${String(year).slice(-2)}${month}`;
              case "YYYYMMDD":
                return `${year}${month}${day}`;
              case "YYMMDD":
                return `${String(year).slice(-2)}${month}${day}`;
              default:
                return `${year}${month}${day}`;
            }
          }

          // 4. 문자
          case "text":
            return autoConfig.textValue || "TEXT";

          default:
            return "XXX";
        }
      });

    return parts.join(config.separator || "");
  }, [config]);

  if (compact) {
    return (
      <div className="bg-muted rounded-md px-2 py-1">
        <code className="text-foreground font-mono text-xs">{generatedCode}</code>
      </div>
    );
  }

  return (
    <div className="bg-muted rounded-md px-3 py-2">
      <code className="text-foreground font-mono text-sm">{generatedCode}</code>
    </div>
  );
};
