"""
Create circular_plate_frequency_tuning_V2 study with all fixes.
"""
from pathlib import Path
import shutil
import json

# Study configuration
study_name = "circular_plate_frequency_tuning_V2"
study_dir = Path("studies") / study_name

# Create study structure
print(f"Creating study: {study_name}")
print("=" * 80)

# 1. Create directory structure
(study_dir / "1_setup" / "model").mkdir(parents=True, exist_ok=True)
(study_dir / "2_results").mkdir(parents=True, exist_ok=True)
(study_dir / "3_reports").mkdir(parents=True, exist_ok=True)

# 2. Copy model files
source_dir = Path("examples/Models/Circular Plate")
model_files = [
    "Circular_Plate.prt",
    "Circular_Plate_sim1.sim",
    "Circular_Plate_fem1.fem",
    "Circular_Plate_fem1_i.prt"
]

print("\n[1/5] Copying model files...")
for file in model_files:
    src = source_dir / file
    dst = study_dir / "1_setup" / "model" / file
    if src.exists():
        shutil.copy2(src, dst)
        print(f"  ✓ {file}")

# 3. Create workflow config
print("\n[2/5] Creating workflow configuration...")
workflow = {
    "study_name": study_name,
    "optimization_request": "Tune the first natural frequency mode to exactly 115 Hz (within 0.1 Hz tolerance)",
    "design_variables": [
        {
            "parameter": "inner_diameter",
            "bounds": [50, 150]
        },
        {
            "parameter": "plate_thickness",
            "bounds": [2, 10]
        }
    ],
    "objectives": [
        {
            "name": "frequency_error",
            "goal": "minimize",
            "extraction": {
                "action": "extract_first_natural_frequency",
                "params": {
                    "mode_number": 1,
                    "target_frequency": 115.0
                }
            }
        }
    ],
    "constraints": [
        {
            "name": "frequency_tolerance",
            "type": "less_than",
            "threshold": 0.1
        }
    ]
}

config_file = study_dir / "1_setup" / "workflow_config.json"
with open(config_file, 'w') as f:
    json.dump(workflow, f, indent=2)
print(f"  ✓ Configuration saved")

print("\n[3/5] Study structure created")
print(f"  Location: {study_dir}")
print(f"  - 1_setup/model: Model files")
print(f"  - 2_results: Optimization results")
print(f"  - 3_reports: Human-readable reports")

print("\n[4/5] Next: Run intelligent setup to generate optimization runner")
print(f"  Command: python create_circular_plate_study.py --study-name {study_name}")

print("\nDone!")
