"""
재질 분류를 위한 공통 스키마
모든 제품군(PIPE, FITTING, FLANGE 등)에서 공통 사용
"""

import re
from typing import Dict, List, Optional

# ========== 미국 ASTM/ASME 규격 ==========
MATERIAL_STANDARDS = {
    "ASTM_ASME": {
        "FORGED_GRADES": {
            "A182": {
                "carbon_alloy": {
                    "patterns": [
                        r"ASTM\s+A182\s+(?:GR\s*)?F(\d+)",
                        r"A182\s+(?:GR\s*)?F(\d+)",
                        r"ASME\s+SA182\s+(?:GR\s*)?F(\d+)"
                    ],
                    "grades": {
                        "F1": {
                            "composition": "0.5Mo",
                            "temp_max": "482°C",
                            "applications": "중온용"
                        },
                        "F5": {
                            "composition": "5Cr-0.5Mo", 
                            "temp_max": "649°C",
                            "applications": "고온용"
                        },
                        "F11": {
                            "composition": "1.25Cr-0.5Mo",
                            "temp_max": "593°C", 
                            "applications": "일반 고온용"
                        },
                        "F22": {
                            "composition": "2.25Cr-1Mo",
                            "temp_max": "649°C",
                            "applications": "고온 고압용"
                        },
                        "F91": {
                            "composition": "9Cr-1Mo-V",
                            "temp_max": "649°C",
                            "applications": "초고온용"
                        }
                    },
                    "manufacturing": "FORGED"
                },
                "stainless": {
                    "patterns": [
                        r"ASTM\s+A182\s+F(\d{3}[LH]*)",
                        r"A182\s+F(\d{3}[LH]*)",
                        r"ASME\s+SA182\s+F(\d{3}[LH]*)"
                    ],
                    "grades": {
                        "F304": {
                            "composition": "18Cr-8Ni",
                            "applications": "일반용",
                            "corrosion_resistance": "보통"
                        },
                        "F304L": {
                            "composition": "18Cr-8Ni-저탄소",
                            "applications": "용접용",
                            "corrosion_resistance": "보통"
                        },
                        "F316": {
                            "composition": "18Cr-10Ni-2Mo",
                            "applications": "내식성",
                            "corrosion_resistance": "우수"
                        },
                        "F316L": {
                            "composition": "18Cr-10Ni-2Mo-저탄소",
                            "applications": "용접+내식성",
                            "corrosion_resistance": "우수"
                        },
                        "F321": {
                            "composition": "18Cr-8Ni-Ti",
                            "applications": "고온안정화",
                            "stabilizer": "Titanium"
                        },
                        "F347": {
                            "composition": "18Cr-8Ni-Nb", 
                            "applications": "고온안정화",
                            "stabilizer": "Niobium"
                        }
                    },
                    "manufacturing": "FORGED"
                }
            },
        
        "A105": {
            "patterns": [
                r"ASTM\s+A105(?:\s+(?:GR\s*)?([ABC]))?",
                r"A105(?:\s+(?:GR\s*)?([ABC]))?",
                r"ASME\s+SA105"
            ],
            "description": "탄소강 단조품",
            "composition": "탄소강",
            "applications": "일반 압력용 단조품",
            "manufacturing": "FORGED",
            "pressure_rating": "150LB ~ 9000LB"
        }
        },
        
        "WELDED_GRADES": {
            "A234": {
                "carbon": {
                    "patterns": [
                        r"ASTM\s+A234\s+(?:GR\s*)?WP([ABC])",
                        r"A234\s+(?:GR\s*)?WP([ABC])",
                        r"ASME\s+SA234\s+(?:GR\s*)?WP([ABC])"
                    ],
                    "grades": {
                        "WPA": {
                            "yield_strength": "30 ksi",
                            "applications": "저압용",
                            "temp_range": "-29°C ~ 400°C"
                        },
                        "WPB": {
                            "yield_strength": "35 ksi", 
                            "applications": "일반용",
                            "temp_range": "-29°C ~ 400°C"
                        },
                        "WPC": {
                            "yield_strength": "40 ksi",
                            "applications": "고압용",
                            "temp_range": "-29°C ~ 400°C"
                        }
                    },
                    "manufacturing": "WELDED_FABRICATED"
                },
                "alloy": {
                    "patterns": [
                        r"ASTM\s+A234\s+(?:GR\s*)?WP(\d+)",
                        r"A234\s+(?:GR\s*)?WP(\d+)",
                        r"ASME\s+SA234\s+(?:GR\s*)?WP(\d+)"
                    ],
                    "grades": {
                        "WP1": {
                            "composition": "0.5Mo",
                            "temp_max": "482°C"
                        },
                        "WP5": {
                            "composition": "5Cr-0.5Mo",
                            "temp_max": "649°C"
                        },
                        "WP11": {
                            "composition": "1.25Cr-0.5Mo",
                            "temp_max": "593°C"
                        },
                        "WP22": {
                            "composition": "2.25Cr-1Mo",
                            "temp_max": "649°C"
                        },
                        "WP91": {
                            "composition": "9Cr-1Mo-V",
                            "temp_max": "649°C"
                        }
                    },
                    "manufacturing": "WELDED_FABRICATED"
                }
            },
            "A403": {
                "stainless": {
                    "patterns": [
                        r"ASTM\s+A403\s+(?:GR\s*)?WP\s*(\d{3}[LH]*)",
                        r"A403\s+(?:GR\s*)?WP\s*(\d{3}[LH]*)",
                        r"ASME\s+SA403\s+(?:GR\s*)?WP\s*(\d{3}[LH]*)",
                        r"ASTM\s+A403\s+(WP\d{3}[LH]*)",
                        r"A403\s+(WP\d{3}[LH]*)",
                        r"(WP\d{3}[LH]*)\s+A403",
                        r"(WP\d{3}[LH]*)"
                    ],
                    "grades": {
                        "WP304": {
                            "base_grade": "304",
                            "manufacturing": "WELDED",
                            "applications": "일반 용접용"
                        },
                        "WP304L": {
                            "base_grade": "304L",
                            "manufacturing": "WELDED", 
                            "applications": "저탄소 용접용"
                        },
                        "WP316": {
                            "base_grade": "316",
                            "manufacturing": "WELDED",
                            "applications": "내식성 용접용"
                        },
                        "WP316L": {
                            "base_grade": "316L",
                            "manufacturing": "WELDED",
                            "applications": "저탄소 내식성 용접용"
                        }
                    },
                    "manufacturing": "WELDED_FABRICATED"
                }
            },
            "A420": {
                "low_temp_carbon": {
                    "patterns": [
                        r"ASTM\s+A420\s+(?:GR\s*)?WPL\s*(\d+)",
                        r"A420\s+(?:GR\s*)?WPL\s*(\d+)",
                        r"ASME\s+SA420\s+(?:GR\s*)?WPL\s*(\d+)",
                        r"ASTM\s+A420\s+(WPL\d+)",
                        r"A420\s+(WPL\d+)",
                        r"(WPL\d+)\s+A420",
                        r"(WPL\d+)"
                    ],
                    "grades": {
                        "WPL1": {
                            "composition": "탄소강",
                            "temp_min": "-29°C",
                            "applications": "저온용 피팅"
                        },
                        "WPL3": {
                            "composition": "3.5Ni",
                            "temp_min": "-46°C", 
                            "applications": "저온용 피팅"
                        },
                        "WPL6": {
                            "composition": "탄소강",
                            "temp_min": "-46°C",
                            "applications": "저온용 피팅"
                        }
                    },
                    "manufacturing": "WELDED_FABRICATED"
                }
            }
        },
        
        "CAST_GRADES": {
            "A216": {
                "patterns": [
                    r"ASTM\s+A216\s+(?:GR\s*)?([A-Z]{2,3})",
                    r"A216\s+(?:GR\s*)?([A-Z]{2,3})",
                    r"ASME\s+SA216\s+(?:GR\s*)?([A-Z]{2,3})"
                ],
                "grades": {
                    "WCA": {
                        "composition": "저탄소강",
                        "applications": "일반주조",
                        "temp_range": "-29°C ~ 425°C"
                    },
                    "WCB": {
                        "composition": "탄소강",
                        "applications": "압력용기",
                        "temp_range": "-29°C ~ 425°C"
                    },
                    "WCC": {
                        "composition": "중탄소강",
                        "applications": "고강도용",
                        "temp_range": "-29°C ~ 425°C"
                    }
                },
                "manufacturing": "CAST"
            },
            "A351": {
                "patterns": [
                    r"ASTM\s+A351\s+(?:GR\s*)?([A-Z0-9]+)",
                    r"A351\s+(?:GR\s*)?([A-Z0-9]+)",
                    r"ASME\s+SA351\s+(?:GR\s*)?([A-Z0-9]+)"
                ],
                "grades": {
                    "CF8": {
                        "base_grade": "304",
                        "manufacturing": "CAST",
                        "applications": "304 스테인리스 주조"
                    },
                    "CF8M": {
                        "base_grade": "316",
                        "manufacturing": "CAST",
                        "applications": "316 스테인리스 주조"
                    },
                    "CF3": {
                        "base_grade": "304L",
                        "manufacturing": "CAST",
                        "applications": "304L 스테인리스 주조"
                    },
                    "CF3M": {
                        "base_grade": "316L",
                        "manufacturing": "CAST",
                        "applications": "316L 스테인리스 주조"
                    }
                },
                "manufacturing": "CAST"
            }
        },
        
        "PIPE_GRADES": {
            "A106": {
                "patterns": [
                    r"ASTM\s+A106\s+(?:GR\s*)?([ABC])",
                    r"A106\s+(?:GR\s*)?([ABC])",
                    r"ASME\s+SA106\s+(?:GR\s*)?([ABC])"
                ],
                "grades": {
                    "A": {
                        "yield_strength": "30 ksi",
                        "applications": "저압용"
                    },
                    "B": {
                        "yield_strength": "35 ksi",
                        "applications": "일반용"
                    },
                    "C": {
                        "yield_strength": "40 ksi", 
                        "applications": "고압용"
                    }
                },
                "manufacturing": "SEAMLESS"
            },
            "A53": {
                "patterns": [
                    r"ASTM\s+A53\s+(?:GR\s*)?([ABC])",
                    r"A53\s+(?:GR\s*)?([ABC])"
                ],
                "grades": {
                    "A": {"yield_strength": "30 ksi"},
                    "B": {"yield_strength": "35 ksi"}
                },
                "manufacturing": "WELDED_OR_SEAMLESS"
            },
            "A312": {
                "patterns": [
                    r"ASTM\s+A312\s+TP\s*(\d{3}[LH]*)",
                    r"A312\s+TP\s*(\d{3}[LH]*)",
                    r"ASTM\s+A312\s+(TP\d{3}[LH]*)",
                    r"A312\s+(TP\d{3}[LH]*)",
                    r"(TP\d{3}[LH]*)\s+A312",
                    r"(TP\d{3}[LH]*)"
                ],
                "grades": {
                    "TP304": {
                        "base_grade": "304",
                        "manufacturing": "SEAMLESS"
                    },
                    "TP304L": {
                        "base_grade": "304L",
                        "manufacturing": "SEAMLESS"
                    },
                    "TP316": {
                        "base_grade": "316",
                        "manufacturing": "SEAMLESS"
                    },
                    "TP316L": {
                        "base_grade": "316L",
                        "manufacturing": "SEAMLESS"
                    }
                },
                "manufacturing": "SEAMLESS"
            },
            "A333": {
                "patterns": [
                    r"ASTM\s+A333\s+(?:GR\s*)?(\d+)",
                    r"A333\s+(?:GR\s*)?(\d+)",
                    r"ASME\s+SA333\s+(?:GR\s*)?(\d+)"
                ],
                "grades": {
                    "1": {
                        "composition": "탄소강",
                        "temp_min": "-29°C",
                        "applications": "저온용 배관"
                    },
                    "3": {
                        "composition": "3.5Ni",
                        "temp_min": "-46°C", 
                        "applications": "저온용 배관"
                    },
                    "6": {
                        "composition": "탄소강",
                        "temp_min": "-46°C",
                        "applications": "저온용 배관"
                    }
                },
                "manufacturing": "SEAMLESS"
            }
        }
    },
    
    # ========== 한국 KS 규격 ==========
    "KS": {
        "PIPE_GRADES": {
            "D3507": {
                "patterns": [r"KS\s+D\s*3507\s+SPPS\s*(\d+)"],
                "description": "배관용 탄소강관",
                "manufacturing": "SEAMLESS"
            },
            "D3583": {
                "patterns": [r"KS\s+D\s*3583\s+STPG\s*(\d+)"],
                "description": "압력배관용 탄소강관",
                "manufacturing": "SEAMLESS"
            },
            "D3576": {
                "patterns": [r"KS\s+D\s*3576\s+STS\s*(\d{3}[LH]*)"],
                "description": "배관용 스테인리스강관",
                "manufacturing": "SEAMLESS"
            }
        },
        "FITTING_GRADES": {
            "D3562": {
                "patterns": [r"KS\s+D\s*3562"],
                "description": "탄소강 단조 피팅",
                "manufacturing": "FORGED"
            },
            "D3563": {
                "patterns": [r"KS\s+D\s*3563"],
                "description": "스테인리스강 단조 피팅",
                "manufacturing": "FORGED"
            }
        }
    },
    
    # ========== 일본 JIS 규격 ==========
    "JIS": {
        "PIPE_GRADES": {
            "G3452": {
                "patterns": [r"JIS\s+G\s*3452\s+SGP"],
                "description": "배관용 탄소강관",
                "manufacturing": "WELDED"
            },
            "G3454": {
                "patterns": [r"JIS\s+G\s*3454\s+STPG\s*(\d+)"],
                "description": "압력배관용 탄소강관",
                "manufacturing": "SEAMLESS"
            },
            "G3459": {
                "patterns": [r"JIS\s+G\s*3459\s+SUS\s*(\d{3}[LH]*)"],
                "description": "배관용 스테인리스강관",
                "manufacturing": "SEAMLESS"
            }
        },
        "FITTING_GRADES": {
            "B2311": {
                "patterns": [r"JIS\s+B\s*2311"],
                "description": "강제 피팅",
                "manufacturing": "FORGED"
            },
            "B2312": {
                "patterns": [r"JIS\s+B\s*2312"],
                "description": "스테인리스강 피팅",
                "manufacturing": "FORGED"
            }
        }
    }
}

# ========== 특수 재질 ==========
SPECIAL_MATERIALS = {
    "SUPER_ALLOYS": {
        "INCONEL": {
            "patterns": [r"INCONEL\s*(\d+)"],
            "grades": {
                "600": {
                    "composition": "Ni-Cr",
                    "temp_max": "1177°C",
                    "applications": "고온 산화 환경"
                },
                "625": {
                    "composition": "Ni-Cr-Mo",
                    "temp_max": "982°C", 
                    "applications": "고온 부식 환경"
                },
                "718": {
                    "composition": "Ni-Cr-Fe",
                    "temp_max": "704°C",
                    "applications": "고온 고강도"
                }
            },
            "manufacturing": "FORGED_OR_CAST"
        },
        "HASTELLOY": {
            "patterns": [r"HASTELLOY\s*([A-Z0-9]+)"],
            "grades": {
                "C276": {
                    "composition": "Ni-Mo-Cr",
                    "corrosion": "최고급",
                    "applications": "강산성 환경"
                },
                "C22": {
                    "composition": "Ni-Cr-Mo-W",
                    "applications": "화학공정"
                }
            },
            "manufacturing": "FORGED_OR_CAST"
        },
        "MONEL": {
            "patterns": [r"MONEL\s*(\d+)"],
            "grades": {
                "400": {
                    "composition": "Ni-Cu",
                    "applications": "해양 환경"
                }
            }
        }
    },
    "TITANIUM": {
        "patterns": [
            r"TITANIUM(?:\s+(?:GR|GRADE)\s*(\d+))?",
            r"Ti(?:\s+(?:GR|GRADE)\s*(\d+))?",
            r"ASTM\s+B\d+\s+(?:GR|GRADE)\s*(\d+)"
        ],
        "grades": {
            "1": {
                "purity": "상업용 순티타늄",
                "strength": "낮음",
                "applications": "화학공정"
            },
            "2": {
                "purity": "상업용 순티타늄 (일반)",
                "strength": "보통",
                "applications": "일반용"
            },
            "5": {
                "composition": "Ti-6Al-4V",
                "strength": "고강도",
                "applications": "항공우주"
            }
        },
        "manufacturing": "FORGED_OR_SEAMLESS"
    },
    "COPPER_ALLOYS": {
        "BRASS": {
            "patterns": [r"BRASS|황동"],
            "composition": "Cu-Zn",
            "applications": ["계기용", "선박용"]
        },
        "BRONZE": {
            "patterns": [r"BRONZE|청동"],
            "composition": "Cu-Sn",
            "applications": ["해양용", "베어링"]
        },
        "CUPRONICKEL": {
            "patterns": [r"Cu-?Ni|CUPRONICKEL"],
            "composition": "Cu-Ni",
            "applications": ["해수 배관"]
        }
    }
}

# ========== 제작방법별 재질 매핑 ==========
MANUFACTURING_MATERIAL_MAP = {
    "FORGED": {
        "primary_standards": ["ASTM A182", "ASTM A105"],
        "size_range": "1/8\" ~ 4\"",
        "pressure_range": "150LB ~ 9000LB",
        "characteristics": "고강도, 고압용"
    },
    "WELDED_FABRICATED": {
        "primary_standards": ["ASTM A234", "ASTM A403"],
        "size_range": "1/2\" ~ 48\"",
        "pressure_range": "150LB ~ 2500LB",
        "characteristics": "대구경, 중저압용"
    },
    "CAST": {
        "primary_standards": ["ASTM A216", "ASTM A351"],
        "size_range": "1/2\" ~ 24\"",
        "applications": ["복잡형상", "밸브본체"],
        "characteristics": "복잡 형상 가능"
    },
    "SEAMLESS": {
        "primary_standards": ["ASTM A106", "ASTM A312", "ASTM A335"],
        "manufacturing": "열간압연/냉간인발",
        "characteristics": "이음새 없는 관"
    },
    "WELDED": {
        "primary_standards": ["ASTM A53", "ASTM A312"],
        "manufacturing": "전기저항용접/아크용접",
        "characteristics": "용접선 있는 관"
    }
}

# ========== 일반 재질 키워드 ==========
GENERIC_MATERIAL_KEYWORDS = {
    "CARBON_STEEL": [
        "CS", "CARBON STEEL", "탄소강", "SS400", "SM490"
    ],
    "STAINLESS_STEEL": [
        "SS", "STS", "STAINLESS", "스테인리스", "304", "316", "321", "347"
    ],
    "ALLOY_STEEL": [
        "ALLOY", "합금강", "CHROME MOLY", "Cr-Mo"
    ],
    "CAST_IRON": [
        "CAST IRON", "주철", "FC", "FCD"
    ],
    "DUCTILE_IRON": [
        "DUCTILE IRON", "구상흑연주철", "덕타일"
    ]
}
