"""Initialize the SQLite schema from SQLModel metadata.

Run from project root: `python migrations/init_db.py`
Creates `fmcg.db` with all 9 tables. Idempotent (create-if-not-exists).
"""

from __future__ import annotations

import sys
from pathlib import Path

PROJECT_ROOT = Path(__file__).resolve().parent.parent
if str(PROJECT_ROOT) not in sys.path:
    sys.path.insert(0, str(PROJECT_ROOT))

from sqlmodel import SQLModel, create_engine  # noqa: E402

import models  # noqa: E402,F401 — registers all tables on SQLModel.metadata

DB_PATH = PROJECT_ROOT / "fmcg.db"


def init_db() -> None:
    engine = create_engine(f"sqlite:///{DB_PATH}")
    SQLModel.metadata.create_all(engine)
    print(f"Schema initialized at {DB_PATH} ({len(SQLModel.metadata.tables)} tables)")


if __name__ == "__main__":
    init_db()
