"""分别检查校验和离散化两个编译阶段。

校验先检查程序语义与公开的 Target 约束，离散化再按指定策略量化受支持字段。
输出保留原值、目标值、误差、单位和对象路径，但不代表已经完成硬件 preflight。
"""

from __future__ import annotations

import json

from cascaqit import AHSProgram, AtomRegister, MockNeutralAtomTarget, Waveform


def main() -> None:
    """基于公开 mock target 校验并离散化一个 waveform。"""
    target = MockNeutralAtomTarget.v0_1()
    program = (
        AHSProgram(
            AtomRegister.line(count=1, spacing=5.0),
            program_id="lesson.compiler.discretization",
        )
        .drive(
            rabi=Waveform.constant(0.8004, duration=0.2004),
            detuning=Waveform.constant(-0.3004, duration=0.2004),
            phase=0.0,
        )
        .measure()
    )
    validated = program.validate(target, shots=16)
    discretized, report = validated.discretize(target, policy="nearest")

    payload = {
        "track": "compiler_engineer",
        "level": "advanced",
        "lesson": "validation_discretization",
        "facts": {
            "validation_errors": [
                item.code for item in validated.diagnostics if item.severity == "error"
            ],
            "policy": report.policy,
            "changed": report.original_program_hash != report.discretized_program_hash,
            "field_changes": [item.to_dict() for item in report.field_reports],
            "field_paths": [item.path for item in report.field_reports],
            "lifecycle": discretized.program_ir.lifecycle_state,
            "hardware_preflight_performed": False,
        },
        "boundaries": {
            "hardware_execution": False,
            "cloud_execution": False,
            "network_accessed": False,
            "credentials_loaded": False,
        },
    }
    print(json.dumps(payload, sort_keys=True))


if __name__ == "__main__":
    main()
