"""Migration validation."""
from pathlib import Path

from app.core.database import Base
import app.models  # noqa: F401


EXPECTED_TABLES = {
    "delivery_organizations",
    "delivery_hubs",
    "delivery_roles",
    "delivery_permissions",
    "external_provider_configs",
    "routing_engine_registrations",
    "delivery_configurations",
    "delivery_settings",
    "delivery_audit_logs",
    "drivers",
    "driver_credentials",
    "driver_documents",
    "driver_lifecycle_events",
    "outbox_events",
    "fleets",
    "vehicle_types",
    "vehicles",
    "vehicle_assignments",
    "driver_availabilities",
    "shifts",
    "shift_assignments",
    "working_zones",
    "pricing_rules",
    "capability_definitions",
    "capability_bundles",
    "dispatch_jobs",
    "job_assignments",
    "route_plans",
    "route_stops",
    "optimization_runs",
    "tracking_sessions",
    "tracking_points",
    "proof_of_delivery",
    "customer_tracking_tokens",
    "settlement_intents",
    "settlement_lines",
}


def test_alembic_revision_exists():
    versions = Path(__file__).resolve().parents[2] / "alembic" / "versions"
    assert list(versions.glob("0001_initial*.py")), "missing 0001_initial migration"
    assert list(versions.glob("0002_phase_101_drivers*.py")), "missing 0002 migration"
    assert list(versions.glob("0003_phase_102_fleet*.py")), "missing 0003 migration"
    assert list(versions.glob("0004_phase_103_availability*.py")), "missing 0004 migration"
    assert list(versions.glob("0005_phase_104_pricing*.py")), "missing 0005 migration"
    assert list(versions.glob("0006_phase_105_dispatch*.py")), "missing 0006 migration"
    assert list(versions.glob("0007_phase_106_routing*.py")), "missing 0007 migration"
    assert list(versions.glob("0008_phase_107_108_tracking_settlement*.py")), (
        "missing 0008 migration"
    )


def test_metadata_has_all_tables():
    names = set(Base.metadata.tables.keys())
    assert EXPECTED_TABLES.issubset(names)


def test_no_journal_entry_table():
    names = set(Base.metadata.tables.keys())
    assert "journal_entries" not in names
    assert "JournalEntry".lower() not in {n.lower() for n in names}
