
import re
from typing import Dict, Optional

def classify_plate(tag: str, description: str, main_nom: str = "") -> Dict:
    """
    판재(PLATE) 분류기
    규격 예: PLATE 10T x 1219 x 2438
    """
    desc_upper = description.upper()
    
    # 1. 두께(Thickness) 추출
    # 패턴: 10T, 10.5T, THK 10, THK. 10, t=10
    thickness = None
    t_match = re.search(r'(\d+(?:\.\d+)?)\s*T\b', desc_upper)
    if not t_match:
        t_match = re.search(r'(?:THK\.?|t=)\s*(\d+(?:\.\d+)?)', desc_upper, re.IGNORECASE)
    
    if t_match:
        thickness = t_match.group(1)
        
    # 2. 규격(Dimensions) 추출
    # 패턴: 1219x2438, 4'x8', 1000*2000
    dimensions = ""
    dim_match = re.search(r'(\d+(?:\.\d+)?)\s*[X\*]\s*(\d+(?:\.\d+)?)(?:\s*[X\*]\s*(\d+(?:\.\d+)?))?', desc_upper)
    if dim_match:
        groups = [g for g in dim_match.groups() if g]
        dimensions = " x ".join(groups)

    # 3. 재질 추출
    material = "UNKNOWN"
    # 압력용기용 및 일반 구조용 강판 재질 추가
    plate_materials = [
        "SUS304", "SUS316", "SUS321", "SS400", "A36", "SM490", 
        "SA516", "A516", "SA283", "A283", "SA537", "A537", "POS-M"
    ]
    for mat in plate_materials:
        if mat in desc_upper:
            material = mat
            break

    return {
        "category": "PLATE",
        "overall_confidence": 0.9,
        "details": {
            "thickness": thickness,
            "dimensions": dimensions,
            "material": material
        }
    }
