#!/usr/bin/env python3
"""Blueprint 변수 검증 조사 스크립트"""
import json
import sys
from pathlib import Path

# Blueprint.json 로드
bp_file = Path("D:/Work/WorldStalker/DS-전투분석_저장소/원본데이터/Blueprint.json")
with open(bp_file, 'r', encoding='utf-8') as f:
    bp_data = json.load(f)

# DataTable.json 로드
dt_file = Path("D:/Work/WorldStalker/DS-전투분석_저장소/원본데이터/DataTable.json")
with open(dt_file, 'r', encoding='utf-8') as f:
    dt_data = json.load(f)

# validated_data.json 로드
val_file = Path("D:/Work/WorldStalker/DS-전투분석_저장소/분석결과/20251024_210822_v2/validated_data.json")
with open(val_file, 'r', encoding='utf-8') as f:
    val_data = json.load(f)

print("=" * 80)
print("Blueprint 변수 검증 조사")
print("=" * 80)

# 예시: Hilda의 SK100202 (반격) 스킬 조사
print("\n=== 예시: Hilda SK100202 (반격) 스킬 ===")

# DT_Skill에서 SK100202 정보
assets = dt_data.get('Assets', [])
dt_skill = [a for a in assets if a.get('AssetName') == 'DT_Skill'][0]
rows = dt_skill.get('Rows', [])
sk100202_row = [r for r in rows if r.get('RowName') == 'SK100202'][0]
sk100202_data = sk100202_row.get('Data', {})

print(f"스킬 이름: {sk100202_data.get('name')}")
print(f"Desc: {sk100202_data.get('desc')}")
print(f"DescValues: {sk100202_data.get('descValues')}")
print(f"AbilityClass: {sk100202_data.get('abilityClass')}")

# Blueprint에서 관련 GA_Skill 찾기
ability_class = sk100202_data.get('abilityClass', '')
if ability_class:
    bp_name = ability_class.split('/')[-1].split('.')[0] if '/' in ability_class else ability_class
    print(f"\nBlueprint 이름: {bp_name}")

    # Blueprint.json에서 검색
    bp_assets = bp_data.get('Assets', [])
    matching_bp = [a for a in bp_assets if a.get('AssetName') == bp_name]

    if matching_bp:
        print(f"Blueprint 발견: {matching_bp[0].get('AssetName')}")

        # 변수 확인
        variables = matching_bp[0].get('BlueprintVariables', [])
        print(f"\nBlueprint 변수 ({len(variables)}개):")
        for var in variables[:10]:  # 처음 10개만
            var_name = var.get('VarName', 'N/A')
            var_type = var.get('VarType', 'N/A')
            default_value = var.get('DefaultValue', 'N/A')
            print(f"  - {var_name} ({var_type}): {default_value}")
    else:
        print(f"Blueprint '{bp_name}' 찾을 수 없음")

# validated_data에서 확인
hilda = val_data['hilda']
sk = hilda['skills']['SK100202']
print(f"\n=== Validated Data ===")
print(f"DescFormatted: {sk.get('descFormatted')}")
print(f"Blueprint Variables in extracted data:")
bp_vars = sk.get('blueprintVariables', {})
if bp_vars:
    for var_name, var_info in list(bp_vars.items())[:5]:
        print(f"  - {var_name}: {var_info}")
else:
    print("  (Blueprint 변수 없음)")

# 다른 스킬도 몇 개 조사
print("\n" + "=" * 80)
print("다른 스킬 샘플 조사")
print("=" * 80)

sample_skills = [
    ('SK100201', 'hilda', '칼날 격돌'),
    ('SK110205', 'urud', '다발 화살'),
    ('SK160202', 'rene', 'Ifrit 소환')
]

for skill_id, stalker, skill_name in sample_skills:
    print(f"\n=== {skill_id} ({skill_name}) ===")

    # DT_Skill
    skill_row = [r for r in rows if r.get('RowName') == skill_id]
    if not skill_row:
        print("  DT_Skill에 없음")
        continue

    skill_data = skill_row[0].get('Data', {})
    desc = skill_data.get('desc', '')
    desc_values = skill_data.get('descValues', [])
    ability_class = skill_data.get('abilityClass', '')

    print(f"  Desc: {desc[:100]}...")
    print(f"  DescValues: {desc_values}")
    print(f"  AbilityClass: {ability_class}")

    # Blueprint
    if ability_class:
        bp_name = ability_class.split('/')[-1].split('.')[0] if '/' in ability_class else ability_class
        matching_bp = [a for a in bp_assets if a.get('AssetName') == bp_name]
        if matching_bp:
            variables = matching_bp[0].get('BlueprintVariables', [])
            print(f"  Blueprint 변수 개수: {len(variables)}")

            # 숫자 타입 변수 찾기
            numeric_vars = [v for v in variables if any(t in str(v.get('VarType', '')) for t in ['Float', 'Int', 'Byte'])]
            if numeric_vars:
                print(f"  숫자 변수 샘플:")
                for var in numeric_vars[:3]:
                    print(f"    - {var.get('VarName')}: {var.get('DefaultValue')}")
