"""Tests for the SQLAlchemy engine + session factory + init_db."""

from __future__ import annotations

from pathlib import Path

import pytest
import sqlalchemy as sa

from cyclone import db


@pytest.fixture(autouse=True)
def _isolated_db(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
    """Point CYCLONE_DB_URL at a temp file for every test in this module."""
    url = f"sqlite:///{tmp_path}/test.db"
    monkeypatch.setenv("CYCLONE_DB_URL", url)
    # Reset module-level state so each test gets a fresh engine.
    db._reset_for_tests()


def test_init_db_creates_engine() -> None:
    db.init_db()
    assert db.engine() is not None
    assert isinstance(db.engine(), sa.Engine)


def test_init_db_creates_all_tables() -> None:
    db.init_db()
    # Probe known future table names from the schema. We use
    # `inspect` rather than importing the model class to keep this
    # test independent of model implementation.
    inspector = sa.inspect(db.engine())
    # In Task 3 we add a Claim table; until then this is empty.
    assert isinstance(inspector.get_table_names(), list)


def test_session_local_returns_session() -> None:
    db.init_db()
    with db.SessionLocal()() as s:
        assert s.is_active
