#!/usr/bin/env python3
"""
Add test Sound Level Meter units to the 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_slms():
    engine = create_engine(DEV_DB_URL, connect_args={"check_same_thread": False})
    SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
    db = SessionLocal()

    test_slms = [
        {
            "id": "nl43-001",
            "device_type": "sound_level_meter",
            "deployed": True,
            "retired": False,
            "note": "Test unit at construction site A",
            "address": "123 Main St, Construction Site A",
            "coordinates": "34.0522,-118.2437",
            "slm_host": "192.168.1.100",
            "slm_tcp_port": 2255,
            "slm_model": "NL-43",
            "slm_serial_number": "123456",
            "slm_frequency_weighting": "A",
            "slm_time_weighting": "F",
            "slm_measurement_range": "30-130 dB",
            "slm_last_check": datetime.utcnow()
        },
        {
            "id": "nl43-002",
            "device_type": "sound_level_meter",
            "deployed": True,
            "retired": False,
            "note": "Test unit at construction site B",
            "address": "456 Oak Ave, Construction Site B",
            "coordinates": "34.0622,-118.2537",
            "slm_host": "192.168.1.101",
            "slm_tcp_port": 2255,
            "slm_model": "NL-43",
            "slm_serial_number": "123457",
            "slm_frequency_weighting": "A",
            "slm_time_weighting": "S",
            "slm_measurement_range": "30-130 dB",
            "slm_last_check": datetime.utcnow()
        },
        {
            "id": "nl53-001",
            "device_type": "sound_level_meter",
            "deployed": True,
            "retired": False,
            "note": "Test unit at residential monitoring",
            "address": "789 Elm St, Residential Area",
            "coordinates": "34.0722,-118.2637",
            "slm_host": "192.168.1.102",
            "slm_tcp_port": 2255,
            "slm_model": "NL-53",
            "slm_serial_number": "234567",
            "slm_frequency_weighting": "C",
            "slm_time_weighting": "F",
            "slm_measurement_range": "25-140 dB",
            "slm_last_check": datetime.utcnow()
        },
        {
            "id": "nl43-003",
            "device_type": "sound_level_meter",
            "deployed": False,
            "retired": False,
            "note": "Benched for calibration",
            "address": None,
            "coordinates": None,
            "slm_host": None,
            "slm_tcp_port": None,
            "slm_model": "NL-43",
            "slm_serial_number": "123458",
            "slm_frequency_weighting": "A",
            "slm_time_weighting": "F",
            "slm_measurement_range": "30-130 dB",
            "slm_last_check": None
        }
    ]

    for slm_data in test_slms:
        # Check if unit already exists
        existing = db.query(RosterUnit).filter_by(id=slm_data["id"]).first()
        if existing:
            print(f"Unit {slm_data['id']} already exists, skipping...")
            continue

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

    db.commit()
    db.close()
    print("\nTest SLM units added successfully to DEV database (data-dev/seismo_fleet.db)!")

if __name__ == "__main__":
    add_test_slms()
