#!/usr/bin/env python3
"""
Add test modem units and assign them to SLMs in DEV database
"""

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from backend.models import RosterUnit
from datetime import datetime

# DEV database
DEV_DB_URL = "sqlite:///./data-dev/seismo_fleet.db"

def add_test_modems():
    engine = create_engine(DEV_DB_URL, connect_args={"check_same_thread": False})
    SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
    db = SessionLocal()

    # Test modems
    test_modems = [
        {
            "id": "modem-001",
            "device_type": "modem",
            "deployed": True,
            "retired": False,
            "note": "Raven XTV modem for Construction Site A",
            "address": "123 Main St, Construction Site A",
            "coordinates": "34.0522,-118.2437",
            "ip_address": "192.168.1.100",
            "phone_number": "+1-555-0100",
            "hardware_model": "Raven XTV"
        },
        {
            "id": "modem-002",
            "device_type": "modem",
            "deployed": True,
            "retired": False,
            "note": "Raven XTV modem for Construction Site B",
            "address": "456 Oak Ave, Construction Site B",
            "coordinates": "34.0622,-118.2537",
            "ip_address": "192.168.1.101",
            "phone_number": "+1-555-0101",
            "hardware_model": "Raven XTV"
        },
        {
            "id": "modem-003",
            "device_type": "modem",
            "deployed": True,
            "retired": False,
            "note": "Sierra Wireless modem for Residential Area",
            "address": "789 Elm St, Residential Area",
            "coordinates": "34.0722,-118.2637",
            "ip_address": "192.168.1.102",
            "phone_number": "+1-555-0102",
            "hardware_model": "Sierra Wireless AirLink"
        },
        {
            "id": "modem-004",
            "device_type": "modem",
            "deployed": False,
            "retired": False,
            "note": "Spare modem - not deployed",
            "ip_address": None,
            "phone_number": "+1-555-0103",
            "hardware_model": "Raven XTV"
        }
    ]

    for modem_data in test_modems:
        # Check if modem already exists
        existing = db.query(RosterUnit).filter_by(id=modem_data["id"]).first()
        if existing:
            print(f"Modem {modem_data['id']} already exists, skipping...")
            continue

        modem = RosterUnit(**modem_data)
        db.add(modem)
        print(f"Added {modem_data['id']}")

    # Assign modems to existing SLMs
    slm_modem_assignments = {
        "nl43-001": "modem-001",
        "nl43-002": "modem-002",
        "nl53-001": "modem-003"
    }

    for slm_id, modem_id in slm_modem_assignments.items():
        slm = db.query(RosterUnit).filter_by(id=slm_id).first()
        if slm:
            slm.deployed_with_modem_id = modem_id
            # Remove legacy slm_host since we're using modems now
            slm.slm_host = None
            print(f"Assigned {slm_id} to {modem_id}")
        else:
            print(f"SLM {slm_id} not found, skipping assignment...")

    db.commit()
    db.close()
    print("\nTest modems added and assigned to SLMs successfully in DEV database (data-dev/seismo_fleet.db)!")
    print("\nModem assignments:")
    for slm_id, modem_id in slm_modem_assignments.items():
        print(f"  {slm_id} → {modem_id}")

if __name__ == "__main__":
    add_test_modems()
