/**
 * 품목 상세 조회 페이지
 */

import { Suspense } from 'react';
import { notFound } from 'next/navigation';
import ItemDetailClient from '@/components/items/ItemDetailClient';
import type { ItemMaster } from '@/types/item';

// Mock 데이터 (API 연동 전 임시)
const mockItems: ItemMaster[] = [
  {
    id: '1',
    itemCode: 'KD-FG-001',
    itemName: '스크린 제품 A',
    itemType: 'FG',
    unit: 'EA',
    specification: '2000x2000',
    isActive: true,
    category1: '본체부품',
    category2: '가이드시스템',
    salesPrice: 150000,
    purchasePrice: 100000,
    marginRate: 33.3,
    processingCost: 20000,
    laborCost: 15000,
    installCost: 10000,
    productCategory: 'SCREEN',
    lotAbbreviation: 'KD',
    note: '스크린 제품 샘플입니다.',
    safetyStock: 10,
    leadTime: 7,
    currentRevision: 0,
    isFinal: false,
    createdAt: '2025-01-10T00:00:00Z',
    updatedAt: '2025-01-12T00:00:00Z',
    bom: [
      {
        id: 'bom-1',
        childItemCode: 'KD-PT-001',
        childItemName: '가이드레일(벽면형)',
        quantity: 2,
        unit: 'EA',
        unitPrice: 35000,
        quantityFormula: 'H / 1000',
      },
      {
        id: 'bom-2',
        childItemCode: 'KD-PT-002',
        childItemName: '절곡품 샘플',
        quantity: 4,
        unit: 'EA',
        unitPrice: 30000,
        isBending: true,
      },
      {
        id: 'bom-3',
        childItemCode: 'KD-SM-001',
        childItemName: '볼트 M6x20',
        quantity: 20,
        unit: 'EA',
        unitPrice: 50,
      },
    ],
  },
  {
    id: '2',
    itemCode: 'KD-PT-001',
    itemName: '가이드레일(벽면형)',
    itemType: 'PT',
    unit: 'EA',
    specification: '2438mm',
    isActive: true,
    category1: '본체부품',
    category2: '가이드시스템',
    category3: '가이드레일',
    salesPrice: 50000,
    purchasePrice: 35000,
    marginRate: 30,
    partType: 'ASSEMBLY',
    partUsage: 'GUIDE_RAIL',
    installationType: '벽면형',
    assemblyType: 'M',
    assemblyLength: '2438',
    currentRevision: 0,
    isFinal: false,
    createdAt: '2025-01-10T00:00:00Z',
  },
  {
    id: '3',
    itemCode: 'KD-PT-002',
    itemName: '절곡품 샘플',
    itemType: 'PT',
    unit: 'EA',
    specification: 'EGI 1.55T',
    isActive: true,
    partType: 'BENDING',
    material: 'EGI 1.55T',
    length: '2000',
    salesPrice: 30000,
    currentRevision: 0,
    isFinal: false,
    createdAt: '2025-01-10T00:00:00Z',
  },
  {
    id: '4',
    itemCode: 'KD-RM-001',
    itemName: 'SPHC-SD',
    itemType: 'RM',
    unit: 'KG',
    specification: '1.6T x 1219 x 2438',
    isActive: true,
    category1: '철강재',
    purchasePrice: 1500,
    material: 'SPHC-SD',
    currentRevision: 0,
    isFinal: false,
    createdAt: '2025-01-10T00:00:00Z',
  },
  {
    id: '5',
    itemCode: 'KD-SM-001',
    itemName: '볼트 M6x20',
    itemType: 'SM',
    unit: 'EA',
    specification: 'M6x20',
    isActive: true,
    category1: '구조재/부속품',
    category2: '볼트/너트',
    purchasePrice: 50,
    currentRevision: 0,
    isFinal: false,
    createdAt: '2025-01-10T00:00:00Z',
  },
];

/**
 * 품목 조회 함수
 * TODO: API 연동 시 fetchItemByCode()로 교체
 */
async function getItemByCode(itemCode: string): Promise<ItemMaster | null> {
  // API 연동 전 mock 데이터 반환
  // const item = await fetchItemByCode(itemCode);
  const item = mockItems.find(
    (item) => item.itemCode === decodeURIComponent(itemCode)
  );
  return item || null;
}

/**
 * 품목 상세 페이지
 */
export default async function ItemDetailPage({
  params,
}: {
  params: Promise<{ id: string }>;
}) {
  const { id } = await params;
  const item = await getItemByCode(id);

  if (!item) {
    notFound();
  }

  return (
    <div className="p-6">
      <Suspense fallback={<div className="text-center py-8">로딩 중...</div>}>
        <ItemDetailClient item={item} />
      </Suspense>
    </div>
  );
}

/**
 * 메타데이터 설정
 */
export async function generateMetadata({
  params,
}: {
  params: Promise<{ id: string }>;
}) {
  const { id } = await params;
  const item = await getItemByCode(id);

  if (!item) {
    return {
      title: '품목을 찾을 수 없습니다',
    };
  }

  return {
    title: `${item.itemName} - 품목 상세`,
    description: `${item.itemCode} 품목 정보`,
  };
}