/**
 * 부품 (PT) 폼 컴포넌트
 * - ASSEMBLY (조립 부품)
 * - BENDING (절곡 부품)
 * - PURCHASED (구매 부품)
 */

import { Label } from '@/components/ui/label';
import {
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue,
} from '@/components/ui/select';
import type { UseFormRegister, UseFormSetValue, UseFormClearErrors, FieldErrors } from 'react-hook-form';
import type { CreateItemFormData } from '@/lib/utils/validation';
import { AssemblyPartForm, BendingPartForm, PurchasedPartForm } from './parts';

export interface PartFormProps {
  // Part Type
  selectedPartType: string;
  setSelectedPartType: (value: string) => void;
  // Category
  selectedCategory1: string;
  setSelectedCategory1: (value: string) => void;
  selectedInstallationType: string;
  setSelectedInstallationType: (value: string) => void;
  // ASSEMBLY
  sideSpecWidth: string;
  setSideSpecWidth: (value: string) => void;
  sideSpecHeight: string;
  setSideSpecHeight: (value: string) => void;
  assemblyLength: string;
  setAssemblyLength: (value: string) => void;
  assemblyUnit: string;
  setAssemblyUnit: (value: string) => void;
  // BENDING
  selectedBendingItemType: string;
  setSelectedBendingItemType: (value: string) => void;
  material: string;
  setMaterial: (value: string) => void;
  widthSum: string;
  setWidthSum: (value: string) => void;
  bendingLength: string;
  setBendingLength: (value: string) => void;
  partUnit: string;
  setPartUnit: (value: string) => void;
  bendingDetailsLength: number;
  // PURCHASED
  electricOpenerPower: string;
  setElectricOpenerPower: (value: string) => void;
  electricOpenerCapacity: string;
  setElectricOpenerCapacity: (value: string) => void;
  motorVoltage: string;
  setMotorVoltage: (value: string) => void;
  chainSpec: string;
  setChainSpec: (value: string) => void;
  // Common
  partStatus: string;
  setPartStatus: (value: string) => void;
  needsBOM: boolean;
  setNeedsBOM: (value: boolean) => void;
  // Item Code Generator
  generateItemCode: () => string;
  // Form
  register: UseFormRegister<CreateItemFormData>;
  setValue: UseFormSetValue<CreateItemFormData>;
  clearErrors: UseFormClearErrors<CreateItemFormData>;
  errors: FieldErrors<CreateItemFormData>;
}

export default function PartForm({
  selectedPartType,
  setSelectedPartType,
  selectedCategory1,
  setSelectedCategory1,
  selectedInstallationType,
  setSelectedInstallationType,
  sideSpecWidth,
  setSideSpecWidth,
  sideSpecHeight,
  setSideSpecHeight,
  assemblyLength,
  setAssemblyLength,
  assemblyUnit,
  setAssemblyUnit,
  selectedBendingItemType,
  setSelectedBendingItemType,
  material,
  setMaterial,
  widthSum,
  setWidthSum,
  bendingLength,
  setBendingLength,
  partUnit,
  setPartUnit,
  bendingDetailsLength,
  electricOpenerPower,
  setElectricOpenerPower,
  electricOpenerCapacity,
  setElectricOpenerCapacity,
  motorVoltage,
  setMotorVoltage,
  chainSpec,
  setChainSpec,
  partStatus,
  setPartStatus,
  needsBOM,
  setNeedsBOM,
  generateItemCode,
  register,
  setValue,
  clearErrors,
  errors,
}: PartFormProps) {
  // 부품 유형 변경 시 필드 초기화 핸들러
  const handlePartTypeChange = (value: string) => {
    setSelectedPartType(value);
    setValue('partType', value);
    clearErrors('partType');

    // 공통 필드 초기화
    setSelectedCategory1('');
    setValue('category1', undefined);
    setPartUnit('EA');
    setValue('unit', 'EA');

    // ASSEMBLY 부품 전용 필드 초기화
    setSelectedInstallationType('');
    setValue('installationType', undefined);
    setSideSpecWidth('');
    setValue('sideSpecWidth', '');
    setSideSpecHeight('');
    setValue('sideSpecHeight', '');
    setAssemblyLength('');
    setValue('assemblyLength', '');
    setAssemblyUnit('EA');

    // BENDING 부품 전용 필드 초기화
    setSelectedBendingItemType('');
    setValue('category2', undefined);
    setMaterial('');
    setValue('material', '');
    setWidthSum('');
    setValue('length', '');
    setBendingLength('');
    setValue('bendingLength', '');

    // PURCHASED 부품 전용 필드 초기화
    setElectricOpenerPower('');
    setValue('electricOpenerPower', '');
    setElectricOpenerCapacity('');
    setValue('electricOpenerCapacity', '');
    setMotorVoltage('');
    setValue('motorVoltage', '');
    setChainSpec('');
    setValue('chainSpec', '');

    // BOM 설정 (절곡 부품은 BOM 없음, 조립 부품은 BOM 기본 true)
    setNeedsBOM(value === 'BENDING' ? false : value === 'ASSEMBLY' ? true : needsBOM);
  };

  return (
    <>
      {/* 부품 유형 선택 - 항상 표시 */}
      <div>
        <Label>
          부품 유형 <span className="text-red-500">*</span>
        </Label>
        <Select
          value={selectedPartType}
          onValueChange={handlePartTypeChange}
        >
          <SelectTrigger className={(errors as any).partType ? 'border-red-500' : ''}>
            <SelectValue placeholder="부품 유형을 선택하세요" />
          </SelectTrigger>
          <SelectContent>
            <SelectItem value="ASSEMBLY">조립 부품 (Assembly Part)</SelectItem>
            <SelectItem value="BENDING">절곡 부품 (Bending Part) - 전개도만 사용</SelectItem>
            <SelectItem value="PURCHASED">구매 부품 (Purchased Part)</SelectItem>
          </SelectContent>
        </Select>
        {(errors as any).partType && (
          <p className="text-xs text-red-500 mt-1">
            {(errors as any).partType.message}
          </p>
        )}
        {!(errors as any).partType && selectedPartType === 'BENDING' && (
          <p className="text-xs text-muted-foreground mt-1">
            * 절곡 부품은 전개도(바라시)만 있으며, 부품 구성(BOM)은 사용하지 않습니다.
          </p>
        )}
      </div>

      {/* ASSEMBLY 부품인 경우 */}
      {selectedPartType === 'ASSEMBLY' && (
        <AssemblyPartForm
          selectedCategory1={selectedCategory1}
          setSelectedCategory1={setSelectedCategory1}
          selectedInstallationType={selectedInstallationType}
          setSelectedInstallationType={setSelectedInstallationType}
          sideSpecWidth={sideSpecWidth}
          setSideSpecWidth={setSideSpecWidth}
          sideSpecHeight={sideSpecHeight}
          setSideSpecHeight={setSideSpecHeight}
          assemblyLength={assemblyLength}
          setAssemblyLength={setAssemblyLength}
          assemblyUnit={assemblyUnit}
          setAssemblyUnit={setAssemblyUnit}
          partStatus={partStatus}
          setPartStatus={setPartStatus}
          needsBOM={needsBOM}
          setNeedsBOM={setNeedsBOM}
          register={register}
          setValue={setValue}
          errors={errors}
        />
      )}

      {/* BENDING 부품인 경우 */}
      {selectedPartType === 'BENDING' && (
        <BendingPartForm
          selectedCategory1={selectedCategory1}
          setSelectedCategory1={setSelectedCategory1}
          selectedBendingItemType={selectedBendingItemType}
          setSelectedBendingItemType={setSelectedBendingItemType}
          material={material}
          setMaterial={setMaterial}
          widthSum={widthSum}
          setWidthSum={setWidthSum}
          bendingLength={bendingLength}
          setBendingLength={setBendingLength}
          partUnit={partUnit}
          setPartUnit={setPartUnit}
          bendingDetailsLength={bendingDetailsLength}
          partStatus={partStatus}
          setPartStatus={setPartStatus}
          generateItemCode={generateItemCode}
          register={register}
          setValue={setValue}
          clearErrors={clearErrors}
          errors={errors}
        />
      )}

      {/* PURCHASED 부품인 경우 */}
      {selectedPartType === 'PURCHASED' && (
        <PurchasedPartForm
          selectedCategory1={selectedCategory1}
          setSelectedCategory1={setSelectedCategory1}
          electricOpenerPower={electricOpenerPower}
          setElectricOpenerPower={setElectricOpenerPower}
          electricOpenerCapacity={electricOpenerCapacity}
          setElectricOpenerCapacity={setElectricOpenerCapacity}
          motorVoltage={motorVoltage}
          setMotorVoltage={setMotorVoltage}
          chainSpec={chainSpec}
          setChainSpec={setChainSpec}
          partUnit={partUnit}
          setPartUnit={setPartUnit}
          partStatus={partStatus}
          setPartStatus={setPartStatus}
          needsBOM={needsBOM}
          setNeedsBOM={setNeedsBOM}
          register={register}
          setValue={setValue}
          errors={errors}
        />
      )}
    </>
  );
}