#!/usr/bin/env python3
"""
Create a fresh test database with the new schema and some sample data.
"""

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from datetime import datetime, date, timedelta
from backend.models import Base, RosterUnit, Emitter

# Create a new test database
TEST_DB_PATH = "/tmp/sfm_test.db"
engine = create_engine(f"sqlite:///{TEST_DB_PATH}", connect_args={"check_same_thread": False})

# Drop all tables and recreate them with the new schema
Base.metadata.drop_all(bind=engine)
Base.metadata.create_all(bind=engine)

# Create a session
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
db = SessionLocal()

try:
    # Add some test seismographs
    seismo1 = RosterUnit(
        id="BE9449",
        device_type="seismograph",
        unit_type="series3",
        deployed=True,
        note="Primary field unit",
        project_id="PROJ-001",
        location="Site A",
        last_calibrated=date(2024, 1, 15),
        next_calibration_due=date(2025, 1, 15),
        deployed_with_modem_id="MDM001",
        last_updated=datetime.utcnow(),
    )

    seismo2 = RosterUnit(
        id="BE9450",
        device_type="seismograph",
        unit_type="series3",
        deployed=False,
        note="Benched for maintenance",
        project_id="PROJ-001",
        location="Warehouse",
        last_calibrated=date(2023, 6, 20),
        next_calibration_due=date(2024, 6, 20),  # Past due
        last_updated=datetime.utcnow(),
    )

    # Add some test modems
    modem1 = RosterUnit(
        id="MDM001",
        device_type="modem",
        unit_type="modem",
        deployed=True,
        note="Paired with BE9449",
        project_id="PROJ-001",
        location="Site A",
        ip_address="192.168.1.100",
        phone_number="+1-555-0123",
        hardware_model="Raven XTV",
        last_updated=datetime.utcnow(),
    )

    modem2 = RosterUnit(
        id="MDM002",
        device_type="modem",
        unit_type="modem",
        deployed=False,
        note="Spare modem",
        project_id="PROJ-001",
        location="Warehouse",
        ip_address="192.168.1.101",
        phone_number="+1-555-0124",
        hardware_model="Raven XT",
        last_updated=datetime.utcnow(),
    )

    # Add test emitters (status reports)
    emitter1 = Emitter(
        id="BE9449",
        unit_type="series3",
        last_seen=datetime.utcnow() - timedelta(hours=2),
        last_file="BE9449.2024.336.12.00.mseed",
        status="OK",
        notes="Running normally",
    )

    emitter2 = Emitter(
        id="BE9450",
        unit_type="series3",
        last_seen=datetime.utcnow() - timedelta(days=30),
        last_file="BE9450.2024.306.08.00.mseed",
        status="Missing",
        notes="No data received",
    )

    # Add all units
    db.add_all([seismo1, seismo2, modem1, modem2, emitter1, emitter2])
    db.commit()

    print(f"✓ Test database created at {TEST_DB_PATH}")
    print(f"✓ Added 2 seismographs (BE9449, BE9450)")
    print(f"✓ Added 2 modems (MDM001, MDM002)")
    print(f"✓ Added 2 emitter status reports")
    print(f"\nDatabase is ready for testing!")

except Exception as e:
    print(f"Error creating test database: {e}")
    db.rollback()
    raise
finally:
    db.close()
