#!/usr/bin/env python3
"""
{{ metadata.title }}

Generated from: {{ metadata.source_file }}
Generated by: kicad-sch-api v{{ metadata.version }}
Date: {{ metadata.date }}
"""

import kicad_sch_api as ksa


def create_{{ metadata.name|sanitize }}():
    """Create {{ metadata.title }} schematic."""

    # Create schematic
    sch = ksa.create_schematic('{{ metadata.name }}')

{% if components %}
    # Add components
{% for comp in components %}
    {{ comp.variable }} = sch.components.add(
        '{{ comp.lib_id }}',
        reference='{{ comp.ref }}',
        value='{{ comp.value }}',
        position=({{ comp.x }}, {{ comp.y }}){% if comp.rotation != 0 %},
        rotation={{ comp.rotation }}{% endif %}

    )
{%- if comp.footprint %}
    {{ comp.variable }}.footprint = '{{ comp.footprint }}'
{%- endif %}
{%- for prop in comp.properties %}
    {{ comp.variable }}.set_property('{{ prop.name }}', '{{ prop.value }}')
{%- endfor %}

{% endfor %}
{% endif %}
{%- if wires %}
    # Add wires
{% for wire in wires %}
    sch.add_wire(
        start=({{ wire.start_x }}, {{ wire.start_y }}),
        end=({{ wire.end_x }}, {{ wire.end_y }})
    )
{% endfor %}

{% endif %}
{%- if labels %}
    # Add labels
{% for label in labels %}
    sch.add_label(
        '{{ label.text }}',
        position=({{ label.x }}, {{ label.y }})
    )
{% endfor %}

{% endif %}
    return sch


if __name__ == '__main__':
    schematic = create_{{ metadata.name|sanitize }}()
    schematic.save('{{ metadata.name }}.kicad_sch')
    print('✅ Schematic generated: {{ metadata.name }}.kicad_sch')
