import logging
import os
import sys
from contextlib import asynccontextmanager
from pathlib import Path
from threading import Lock

if __package__ in {None, ""}:
    sys.path.insert(0, str(Path(__file__).resolve().parents[1]))

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
import uvicorn

from app.api.access import router as access_router
from app.api.auth import router as auth_router
from app.api.client_access import router as client_access_router
from app.api.dashboard import router as dashboard_router
from app.api.mix_calculator import router as mix_calculator_router
from app.api.mixes import router as mixes_router
from app.api.powerbi import router as powerbi_router
from app.api.products import router as products_router
from app.api.raw_materials import router as raw_materials_router
from app.api.scenarios import router as scenarios_router
from app.core.config import settings
from app.db.session import Base, engine
from app.db.migrations import MigrationReport, bootstrap_schema, sync_tenant_ids
from app.seed import seed_if_empty

logger = logging.getLogger("data_entry_app.startup")
_database_ready = False
_database_ready_lock = Lock()


def ensure_database_ready() -> MigrationReport:
    global _database_ready

    if _database_ready:
        return MigrationReport()

    with _database_ready_lock:
        if _database_ready:
            return MigrationReport()

        schema_report = bootstrap_schema(engine, Base.metadata)
        seed_if_empty()
        tenant_sync_report = sync_tenant_ids(engine)

        report = MigrationReport(
            created_tables=schema_report.created_tables,
            added_columns=schema_report.added_columns,
            synced_tenant_rows=tenant_sync_report,
        )
        logger.info("Database startup checks complete: %s", report.summary())
        _database_ready = True
        return report


@asynccontextmanager
async def lifespan(_: FastAPI):
    ensure_database_ready()
    yield


app = FastAPI(title=settings.app_name, lifespan=lifespan)

app.add_middleware(
    CORSMiddleware,
    allow_origins=list(settings.cors_allow_origins),
    allow_origin_regex=settings.cors_allow_origin_regex,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

app.include_router(auth_router)
app.include_router(access_router)
app.include_router(client_access_router)
app.include_router(dashboard_router)
app.include_router(raw_materials_router)
app.include_router(mixes_router)
app.include_router(mix_calculator_router)
app.include_router(products_router)
app.include_router(scenarios_router)
app.include_router(powerbi_router)


@app.get("/")
def root():
    return {
        "app": settings.app_name,
        "message": "Use the operator frontend to sign in, manage raw materials, and review downstream mix and product costs.",
        "workflow": [
            "Sign in as an operator",
            "Update raw material prices or add new materials",
            "Review mix master recalculations",
            "Confirm finished product pricing outputs",
        ],
        "endpoints": {
            "client_login": "/api/auth/client/login",
            "admin_login": "/api/auth/admin/login",
            "raw_materials": "/api/raw-materials",
            "mixes": "/api/mixes",
            "mix_calculator": "/api/mix-calculator",
            "products": "/api/products",
            "scenarios": "/api/scenarios",
            "client_access": "/api/client-access",
            "docs": "/docs",
        },
    }


@app.get("/health")
def healthcheck():
    return {"status": "ok"}


if __name__ == "__main__":
    report = ensure_database_ready()
    print(f"Database startup checks complete: {report.summary()}")
    uvicorn.run(
        app,
        host=os.getenv("HOST", "0.0.0.0"),
        port=int(os.getenv("PORT", "8000")),
    )
