#!/usr/bin/env python3
"""Run migrations and seed templates. Called at container startup."""
import asyncio
import subprocess
import sys


def run_migrations():
    result = subprocess.run(
        ["alembic", "upgrade", "head"],
        capture_output=True,
        text=True,
    )
    if result.returncode != 0:
        print(f"Migration failed:\n{result.stderr}", file=sys.stderr)
        sys.exit(1)
    print(result.stdout)


async def main():
    run_migrations()
    from app.config import settings
    from app.utils.seed_templates import seed
    await seed(settings.database_url)


if __name__ == "__main__":
    asyncio.run(main())
