"""
Seed-Script: Erzeugt CustomModule-Vorlage "SAS Meckenbeuren"
mit vollständigem Formular- und Regelwerk.
"""
import sys, os, json
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))

from app import create_app
from app.extensions import db
from app.models.custom_module import CustomModule

app = create_app()

FORM_JSON = [
    # ── HA (Hausanschluss) ──
    {"type":"group_start","title":"Hausanschluss","collapsible":False},
    {"type":"checkbox","name":"ha_herstellen","label":"HA Herstellen"},
    {"type":"text","name":"scan_name_ha","label":"Scan Name","placeholder":"Scan Name"},
    {"type":"number","name":"trassenmeter","label":"Trassenmeter","inputmode":"decimal","step":"0.1"},
    {"type":"number","name":"kabelmeter_ha","label":"Kabelsichern (m)","inputmode":"decimal","step":"0.1"},
    {"type":"number","name":"anz_qkr","label":"Anzahl Q-Kabel/Rohre","inputmode":"numeric","step":"1"},
    {"type":"number","name":"anz_qst","label":"Anzahl Querungsstellen","inputmode":"numeric","step":"1"},
    {"type":"number","name":"strqm","label":"Straßenquerung (m)","inputmode":"decimal","step":"0.1"},
    {"type":"number","name":"anz_suchgrube_ha","label":"Anzahl Suchgrube","inputmode":"numeric","step":"1"},
    {"type":"checkbox","name":"einzug_10er","label":"Einzug 10er Pipes"},
    {"type":"group_end"},

    # ── TB (Tiefbau) ──
    {"type":"group_start","title":"Tiefbau","collapsible":False},
    {"type":"text","name":"scan_name_tb","label":"Scan Name","placeholder":"Scan Name"},
    {"type":"number","name":"tb_laenge","label":"Länge (m)","inputmode":"decimal","step":"0.1"},
    {"type":"number","name":"tb_tiefe","label":"Tiefe (m)","inputmode":"decimal","step":"0.1"},
    {"type":"checkbox","name":"tb_unbefestigt","label":"unbefestigt (Wiese, Kies)"},
    {"type":"checkbox","name":"tb_befestigt","label":"befestigt (Pflaster, Asphalt)"},
    {"type":"number","name":"tb_anz_12x10","label":"Anzahl Rohre 12×10","inputmode":"numeric","step":"1"},
    {"type":"number","name":"tb_anz_4x20","label":"Anzahl Rohre 4×20","inputmode":"numeric","step":"1"},
    {"type":"number","name":"tb_anz_qk","label":"Anzahl Q-Kabel/Rohre","inputmode":"numeric","step":"1"},
    {"type":"number","name":"tb_anz_qs","label":"Anzahl Querungsst.","inputmode":"numeric","step":"1"},
    {"type":"number","name":"tb_kabelmeter","label":"Kabelsichern (m)","inputmode":"decimal","step":"0.1"},
    {"type":"number","name":"tb_anz_suchgrube","label":"Anzahl Suchgrube","inputmode":"numeric","step":"1"},
    {"type":"checkbox","name":"tb_kg2","label":"Kabelgraben 2"},
    {"type":"checkbox","name":"tb_kg4","label":"Kabelgraben 4"},
    {"type":"checkbox","name":"tb_kg6","label":"Kabelgraben 6"},
    {"type":"group_end"},
]

RULES_JSON = [
    # ════════════════════════════════════════
    # HA (Hausanschluss)
    # ════════════════════════════════════════

    # HA Basis
    {"name":"HA – Basispositionen","conditions":{"operator":"and","items":[
        {"field":"ha_herstellen","operator":"is_checked"}]},
     "actions":[
        {"pos_nr":"01.06.0001","columns":{"menge":{"type":"fixed","value":"1"}}},
        {"pos_nr":"01.06.0003","columns":{"menge":{"type":"fixed","value":"1"}}},
        {"pos_nr":"01.06.0007","columns":{"menge":{"type":"fixed","value":"1"}}}]},

    # HA Q-Kabel/Querung
    {"name":"HA – Q-Kabel / Querung","conditions":{"operator":"and","items":[
        {"field":"ha_herstellen","operator":"is_checked"},
        {"field":"anz_qkr","operator":"gt","value":"0"}]},
     "actions":[
        {"pos_nr":"01.03.0019","columns":{"faktor":{"type":"field","value":"anz_qst"},"laenge":{"type":"fixed","value":"1"},"menge":{"type":"fixed","value":"1"},"bemerkung":{"type":"fixed","value":"Siehe Bild: "}}},
        {"pos_nr":"01.03.0020","columns":{"faktor":{"type":"field","value":"anz_qkr"},"laenge":{"type":"fixed","value":"0.5"},"menge":{"type":"fixed","value":"0.5"},"bemerkung":{"type":"fixed","value":"Siehe Bild: "}}}]},

    # HA Trassenmeter
    {"name":"HA – Trassenmeter","conditions":{"operator":"and","items":[
        {"field":"ha_herstellen","operator":"is_checked"},
        {"field":"trassenmeter","operator":"gt","value":"0"}]},
     "actions":[
        {"pos_nr":"01.06.0004","columns":{"laenge":{"type":"field","value":"trassenmeter"}}}]},

    # HA Einzug 10er
    {"name":"HA – Einzug 10er Pipes","conditions":{"operator":"and","items":[
        {"field":"ha_herstellen","operator":"is_checked"},
        {"field":"trassenmeter","operator":"gt","value":"0"},
        {"field":"einzug_10er","operator":"is_checked"}]},
     "actions":[
        {"pos_nr":"01.04.0003","columns":{"laenge":{"type":"field","value":"trassenmeter"},"menge":{"type":"field","value":"trassenmeter"}}},
        {"pos_nr":"01.06.0006","columns":{"laenge":{"type":"field","value":"trassenmeter"},"menge":{"type":"field","value":"trassenmeter"}}}]},

    # HA ohne Einzug
    {"name":"HA – ohne Einzug","conditions":{"operator":"and","items":[
        {"field":"ha_herstellen","operator":"is_checked"},
        {"field":"trassenmeter","operator":"gt","value":"0"},
        {"field":"einzug_10er","operator":"is_empty"}]},
     "actions":[
        {"pos_nr":"01.06.0006","columns":{"laenge":{"type":"formula","value":"[trassenmeter]+1+[strqm]"}}}]},

    # HA Kabelsichern
    {"name":"HA – Kabelsichern","conditions":{"operator":"and","items":[
        {"field":"ha_herstellen","operator":"is_checked"},
        {"field":"kabelmeter_ha","operator":"gt","value":"0"}]},
     "actions":[
        {"pos_nr":"01.03.0019","columns":{"laenge":{"type":"field","value":"kabelmeter_ha"},"menge":{"type":"field","value":"kabelmeter_ha"},"bemerkung":{"type":"fixed","value":"Siehe Bild: "}}},
        {"pos_nr":"01.03.0020","columns":{"laenge":{"type":"field","value":"kabelmeter_ha"},"menge":{"type":"field","value":"kabelmeter_ha"},"bemerkung":{"type":"fixed","value":"Siehe Bild: "}}}]},

    # HA Straßenquerung
    {"name":"HA – Straßenquerung","conditions":{"operator":"and","items":[
        {"field":"ha_herstellen","operator":"is_checked"},
        {"field":"strqm","operator":"gt","value":"0"}]},
     "actions":[
        {"pos_nr":"01.03.0008","columns":{"laenge":{"type":"field","value":"strqm"},"menge":{"type":"field","value":"strqm"},"bemerkung":{"type":"fixed","value":"Öffentlicherbereich "}}}]},

    # HA Suchgrube
    {"name":"HA – Suchgrube","conditions":{"operator":"and","items":[
        {"field":"ha_herstellen","operator":"is_checked"},
        {"field":"anz_suchgrube_ha","operator":"gt","value":"0"}]},
     "actions":[
        {"pos_nr":"01.03.0018","columns":{"menge":{"type":"field","value":"anz_suchgrube_ha"},"bemerkung":{"type":"fixed","value":"Öffentlicherbereich "}}}]},

    # ════════════════════════════════════════
    # TB (Tiefbau)
    # ════════════════════════════════════════

    # -- Tiefe ≤ 0,65 --
    {"name":"TB 0,6m – unbefestigt","conditions":{"operator":"and","items":[
        {"field":"tb_laenge","operator":"gt","value":"0"},
        {"field":"tb_tiefe","operator":"gt","value":"0"},
        {"field":"tb_tiefe","operator":"lte","value":"0.65"},
        {"field":"tb_unbefestigt","operator":"is_checked"}]},
     "actions":[
        {"pos_nr":"01.03.0001","columns":{"laenge":{"type":"field","value":"tb_laenge"}}}]},

    {"name":"TB 0,6m – KG 2","conditions":{"operator":"and","items":[
        {"field":"tb_laenge","operator":"gt","value":"0"},
        {"field":"tb_tiefe","operator":"gt","value":"0"},
        {"field":"tb_tiefe","operator":"lte","value":"0.65"},
        {"field":"tb_befestigt","operator":"is_checked"},
        {"field":"tb_kg2","operator":"is_checked"}]},
     "actions":[
        {"pos_nr":"01.03.0004","columns":{"laenge":{"type":"field","value":"tb_laenge"}}}]},

    {"name":"TB 0,6m – KG 4","conditions":{"operator":"and","items":[
        {"field":"tb_laenge","operator":"gt","value":"0"},
        {"field":"tb_tiefe","operator":"gt","value":"0"},
        {"field":"tb_tiefe","operator":"lte","value":"0.65"},
        {"field":"tb_befestigt","operator":"is_checked"},
        {"field":"tb_kg4","operator":"is_checked"}]},
     "actions":[
        {"pos_nr":"01.03.0005","columns":{"laenge":{"type":"field","value":"tb_laenge"}}}]},

    {"name":"TB 0,6m – KG 6","conditions":{"operator":"and","items":[
        {"field":"tb_laenge","operator":"gt","value":"0"},
        {"field":"tb_tiefe","operator":"gt","value":"0"},
        {"field":"tb_tiefe","operator":"lte","value":"0.65"},
        {"field":"tb_befestigt","operator":"is_checked"},
        {"field":"tb_kg6","operator":"is_checked"}]},
     "actions":[
        {"pos_nr":"01.03.0006","columns":{"laenge":{"type":"field","value":"tb_laenge"}}}]},

    # -- Tiefe ≤ 0,9 --
    {"name":"TB 0,8m – unbefestigt","conditions":{"operator":"and","items":[
        {"field":"tb_laenge","operator":"gt","value":"0"},
        {"field":"tb_tiefe","operator":"gt","value":"0.65"},
        {"field":"tb_tiefe","operator":"lte","value":"0.9"},
        {"field":"tb_unbefestigt","operator":"is_checked"}]},
     "actions":[
        {"pos_nr":"01.03.0002","columns":{"laenge":{"type":"field","value":"tb_laenge"}}}]},

    {"name":"TB 0,8m – KG 2","conditions":{"operator":"and","items":[
        {"field":"tb_laenge","operator":"gt","value":"0"},
        {"field":"tb_tiefe","operator":"gt","value":"0.65"},
        {"field":"tb_tiefe","operator":"lte","value":"0.9"},
        {"field":"tb_befestigt","operator":"is_checked"},
        {"field":"tb_kg2","operator":"is_checked"}]},
     "actions":[
        {"pos_nr":"01.03.0008","columns":{"laenge":{"type":"field","value":"tb_laenge"}}}]},

    {"name":"TB 0,8m – KG 4","conditions":{"operator":"and","items":[
        {"field":"tb_laenge","operator":"gt","value":"0"},
        {"field":"tb_tiefe","operator":"gt","value":"0.65"},
        {"field":"tb_tiefe","operator":"lte","value":"0.9"},
        {"field":"tb_befestigt","operator":"is_checked"},
        {"field":"tb_kg4","operator":"is_checked"}]},
     "actions":[
        {"pos_nr":"01.03.0009","columns":{"laenge":{"type":"field","value":"tb_laenge"}}}]},

    {"name":"TB 0,8m – KG 6","conditions":{"operator":"and","items":[
        {"field":"tb_laenge","operator":"gt","value":"0"},
        {"field":"tb_tiefe","operator":"gt","value":"0.65"},
        {"field":"tb_tiefe","operator":"lte","value":"0.9"},
        {"field":"tb_befestigt","operator":"is_checked"},
        {"field":"tb_kg6","operator":"is_checked"}]},
     "actions":[
        {"pos_nr":"01.03.0010","columns":{"laenge":{"type":"field","value":"tb_laenge"}}}]},

    # -- Tiefe > 0,9 --
    {"name":"TB 1,2m – unbefestigt","conditions":{"operator":"and","items":[
        {"field":"tb_laenge","operator":"gt","value":"0"},
        {"field":"tb_tiefe","operator":"gt","value":"0.9"},
        {"field":"tb_unbefestigt","operator":"is_checked"}]},
     "actions":[
        {"pos_nr":"01.03.0003","columns":{"laenge":{"type":"field","value":"tb_laenge"}}}]},

    {"name":"TB 1,2m – KG 2","conditions":{"operator":"and","items":[
        {"field":"tb_laenge","operator":"gt","value":"0"},
        {"field":"tb_tiefe","operator":"gt","value":"0.9"},
        {"field":"tb_befestigt","operator":"is_checked"},
        {"field":"tb_kg2","operator":"is_checked"}]},
     "actions":[
        {"pos_nr":"01.03.0012","columns":{"laenge":{"type":"field","value":"tb_laenge"}}}]},

    {"name":"TB 1,2m – KG 4","conditions":{"operator":"and","items":[
        {"field":"tb_laenge","operator":"gt","value":"0"},
        {"field":"tb_tiefe","operator":"gt","value":"0.9"},
        {"field":"tb_befestigt","operator":"is_checked"},
        {"field":"tb_kg4","operator":"is_checked"}]},
     "actions":[
        {"pos_nr":"01.03.0013","columns":{"laenge":{"type":"field","value":"tb_laenge"}}}]},

    {"name":"TB 1,2m – KG 6","conditions":{"operator":"and","items":[
        {"field":"tb_laenge","operator":"gt","value":"0"},
        {"field":"tb_tiefe","operator":"gt","value":"0.9"},
        {"field":"tb_befestigt","operator":"is_checked"},
        {"field":"tb_kg6","operator":"is_checked"}]},
     "actions":[
        {"pos_nr":"01.03.0014","columns":{"laenge":{"type":"field","value":"tb_laenge"}}}]},

    # TB Rohre
    {"name":"TB – Rohre 4×20","conditions":{"operator":"and","items":[
        {"field":"tb_laenge","operator":"gt","value":"0"},
        {"field":"tb_tiefe","operator":"gt","value":"0"},
        {"field":"tb_anz_4x20","operator":"gt","value":"0"}]},
     "actions":[
        {"pos_nr":"01.04.0001","columns":{"faktor":{"type":"field","value":"tb_anz_4x20"},"laenge":{"type":"field","value":"tb_laenge"},"menge":{"type":"field","value":"tb_laenge"},"bemerkung":{"type":"fixed","value":"4x20 Rohre"}}}]},

    {"name":"TB – Rohre 12×10","conditions":{"operator":"and","items":[
        {"field":"tb_laenge","operator":"gt","value":"0"},
        {"field":"tb_tiefe","operator":"gt","value":"0"},
        {"field":"tb_anz_12x10","operator":"gt","value":"0"}]},
     "actions":[
        {"pos_nr":"01.04.0002","columns":{"faktor":{"type":"field","value":"tb_anz_12x10"},"laenge":{"type":"field","value":"tb_laenge"},"menge":{"type":"field","value":"tb_laenge"},"bemerkung":{"type":"fixed","value":"12x10 Rohre"}}}]},

    # TB Querung
    {"name":"TB – Querung","conditions":{"operator":"and","items":[
        {"field":"tb_anz_qs","operator":"gt","value":"0"}]},
     "actions":[
        {"pos_nr":"01.03.0019","columns":{"faktor":{"type":"field","value":"tb_anz_qs"},"laenge":{"type":"fixed","value":"1"},"menge":{"type":"fixed","value":"1"},"bemerkung":{"type":"fixed","value":"Siehe Bild: "}}},
        {"pos_nr":"01.03.0020","columns":{"faktor":{"type":"field","value":"tb_anz_qk"},"laenge":{"type":"fixed","value":"0.5"},"menge":{"type":"fixed","value":"0.5"},"bemerkung":{"type":"fixed","value":"Siehe Bild: "}}}]},

    # TB Kabelsichern
    {"name":"TB – Kabelsichern","conditions":{"operator":"and","items":[
        {"field":"tb_kabelmeter","operator":"gt","value":"0"}]},
     "actions":[
        {"pos_nr":"01.03.0019","columns":{"laenge":{"type":"field","value":"tb_kabelmeter"},"menge":{"type":"field","value":"tb_kabelmeter"},"bemerkung":{"type":"fixed","value":"Siehe Bild: "}}},
        {"pos_nr":"01.03.0020","columns":{"laenge":{"type":"field","value":"tb_kabelmeter"},"menge":{"type":"field","value":"tb_kabelmeter"},"bemerkung":{"type":"fixed","value":"Siehe Bild: "}}}]},

    # TB Suchgrube
    {"name":"TB – Suchgrube","conditions":{"operator":"and","items":[
        {"field":"tb_anz_suchgrube","operator":"gt","value":"0"}]},
     "actions":[
        {"pos_nr":"01.03.0018","columns":{"menge":{"type":"field","value":"tb_anz_suchgrube"},"bemerkung":{"type":"fixed","value":"Öffentlicherbereich "}}}]},
]

def run():
    with app.app_context():
        name = 'SAS Meckenbeuren'
        existing = CustomModule.query.filter_by(name=name, is_template=True).first()
        if existing:
            existing.form_json = json.dumps(FORM_JSON, ensure_ascii=False)
            existing.rules_json = json.dumps(RULES_JSON, ensure_ascii=False)
            existing.description = 'SAS Meckenbeuren – Hausanschluss + Tiefbau (portiert vom Legacy-Modul)'
            db.session.commit()
            print(f'OK - Vorlage "{name}" aktualisiert (ID {existing.id})')
            print(f'   Formular-Felder: {len(FORM_JSON)}')
            print(f'   Regeln: {len(RULES_JSON)}')
            return

        mod = CustomModule(
            name=name,
            description='SAS Meckenbeuren – Hausanschluss + Tiefbau (portiert vom Legacy-Modul)',
            kategorie='Spezial',
            icon='📍',
            form_json=json.dumps(FORM_JSON, ensure_ascii=False),
            rules_json=json.dumps(RULES_JSON, ensure_ascii=False),
            is_template=True,
            sort_index=0,
            is_active=True,
        )
        db.session.add(mod)
        db.session.commit()
        print(f'OK - Vorlage "{name}" angelegt (ID {mod.id})')
        print(f'   Formular-Felder: {len(FORM_JSON)}')
        print(f'   Regeln: {len(RULES_JSON)}')

if __name__ == '__main__':
    run()
