# flake8: noqa T001, E501
# -*- coding: utf-8 -*-
"""
Metadata Usage Example

Access DUT and test plan information using simple helper methods.
"""

import BTAnalysisPyInterface as bta


def main():
    print("Reading metadata...")

    for source in bta.get_sources():
        # Get DUT information
        dut_name = source.get_dut_type_name()

        dut_mass = source.get_dut_type_mass()
        dut_mass_unit = source.get_dut_type_mass_unit()

        dut_mass_from_test_run = source.get_dut_type_mass(True)
        dut_mass_from_test_run_unit = source.get_dut_type_mass_unit(True)

        dut_capacity = source.get_dut_type_capacity()
        dut_capacity_unit = source.get_dut_type_capacity_unit()

        dut_volume = source.get_dut_type_volume()
        dut_volume_unit = source.get_dut_type_volume_unit()

        dut_classification = source.get_dut_type_classification()
        dut_producer = source.get_dut_type_producer()

        # Get voltage limits
        v_nominal = source.get_dut_type_nominal_voltage()
        v_nominal_unit = source.get_dut_type_nominal_voltage_unit()

        v_min = source.get_dut_type_min_voltage()
        v_min_unit = source.get_dut_type_min_voltage_unit()

        v_max = source.get_dut_type_max_voltage()
        v_max_unit = source.get_dut_type_max_voltage_unit()

        # Get test plan information
        test_name = source.get_test_plan_name()
        test_mode = source.get_test_plan_cycle_mode()
        hardware = source.get_test_plan_hardware_type()
        test_id = source.get_test_plan_id()
        tags = source.get_test_plan_tags()

        # Display DUT info
        print(f"=== DUT Information ===")
        print(f"Name: {dut_name}")
        if dut_classification:
            print(f"Classification: {dut_classification}")
        if dut_producer:
            print(f"Producer: {dut_producer}")
        if dut_mass > 0:
            print(f"Mass: {dut_mass:.3f} {dut_mass_unit}")
        if dut_mass_from_test_run > 0:
            print(f"Mass: {dut_mass_from_test_run:.3f} {dut_mass_from_test_run_unit}")
        if dut_capacity > 0:
            print(f"Nominal capacity: {dut_capacity:.3f} {dut_capacity_unit}")
        if dut_volume > 0:
            print(f"Volume: {dut_volume} {dut_volume_unit}")

        # Display voltage limits
        if v_nominal > 0:
            print(f"Nominal voltage: {v_nominal:.3f} {v_nominal_unit}")
        if v_min > 0:
            print(f"Min voltage: {v_min:.3f} {v_min_unit}")
        if v_max > 0:
            print(f"Max voltage: {v_max:.3f} {v_max_unit}")

        # Display test plan info
        print(f"=== Test Plan Information ===")
        print(f"Test: {test_name}")
        print(f"ID: {test_id}")
        if test_mode:
            print(f"Cycle mode: {test_mode}")
        if hardware:
            print(f"Hardware: {hardware}")
        if tags:
            print(f"Tags: {', '.join(tags)}")

        # Example: Calculate gravimetric capacity
        if dut_mass > 0 and dut_capacity > 0:
            gravimetric = dut_capacity / dut_mass
            print(
                f"Gravimetric capacity: {gravimetric:.2f} {dut_capacity_unit}/{dut_mass_unit}"
            )

        # Example: Calculate volumetric capacity
        if dut_volume > 0 and dut_capacity > 0:
            volumetric = dut_capacity / dut_volume
            print(
                f"Volumetric capacity: {volumetric:.2f} {dut_capacity_unit}/{dut_volume_unit}"
            )

        # Example: Voltage safety check
        if v_min > 0 and v_max > 0:
            print(f"Voltage limits: {v_min} {v_min_unit} - {v_max} {v_max_unit}")

            # Check first few voltage measurements
            count = 0
            violations = 0
            for point in source.data_points("Potentiostat measure", ["U"]):
                if point["U"] < v_min or point["U"] > v_max:
                    violations += 1
                count += 1
                if count >= 100:  # Check first 100 points
                    break

            if violations > 0:
                print(
                    f"⚠️ Found {violations} voltage violations in first {count} points"
                )
            else:
                print(f"✓ All voltages within limits (checked {count} points)")
