#!/usr/bin/env python3
from pathlib import Path
import pandas as pd
import numpy as np

ROOT = Path(__file__).resolve().parents[1]
site_candidates = ROOT / "data" / "site_metadata" / "tokamak_site_metadata_v2_vector_class_candidates.csv"
site_schema = ROOT / "data" / "site_metadata" / "tokamak_site_metadata_v1_vector_class_schema.csv"
out_path = ROOT / "outputs" / "vector_class_gate_audit.csv"

site_path = site_candidates if site_candidates.exists() else site_schema
site = pd.read_csv(site_path)

site["has_hall_axis"] = pd.to_numeric(site.get("hall_axis_azimuth_deg"), errors="coerce").notna()
site["has_machine_axis"] = pd.to_numeric(site.get("machine_axis_azimuth_deg"), errors="coerce").notna()
site["axis_is_physics_vector_true_false"] = site.get("axis_is_physics_vector_true_false", False).astype(str).str.lower().isin(["true", "1", "yes"])
site["has_transfer"] = site[["gamma_lab", "gamma_wall", "gamma_b"]].notna().all(axis=1)
site["can_compute_parent_Rphi"] = site["has_machine_axis"] & site["axis_is_physics_vector_true_false"] & site["has_transfer"]
site["hall_axis_not_diagnostic"] = site["has_hall_axis"] & ~site["has_machine_axis"]

cols = [
    "TOK", "valid_benchmark_rows", "has_hall_axis", "has_machine_axis",
    "axis_is_physics_vector_true_false", "has_transfer", "can_compute_parent_Rphi",
    "hall_axis_not_diagnostic", "diagnostic_status", "axis_promotion_status"
]
site[cols].to_csv(out_path, index=False)
print(site[cols].to_string(index=False))
