#!/usr/bin/env python
"""
WSGI entry point for local development.

Usage:
    python wsgi.py

This starts the Flask development server with the Semantic Models backend.
The server reads configuration from .env file in the project root.

For frontend development, run in a separate terminal:
    cd resource/frontend && yarn dev
"""

import os
import sys

# Add python-lib to path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "python-lib"))

# Load environment variables from .env file
from dotenv import load_dotenv

load_dotenv()

# Set LOCAL_DEV if not already set
if not os.getenv("LOCAL_DEV"):
    os.environ["LOCAL_DEV"] = "true"

import semantic_models.routes.semantic_models as semantic_models_routes
from flask import Flask


def _dev_user():
    return os.environ["DSS_DEV_USER"]


from semantic_models.setup import setup_app

semantic_models_routes.get_current_user = _dev_user


def create_app() -> Flask:
    """Create and configure the Flask application."""
    app = Flask(__name__)
    setup_app(app)
    return app


if __name__ == "__main__":
    # Get port from environment
    port = int(os.getenv("VITE_API_PORT", "5000"))
    debug = True

    print(f"\n{'=' * 60}")
    print("Semantic Models - Local Development Server")
    print(f"{'=' * 60}")
    print(f"Backend URL: http://127.0.0.1:{port}")
    print(f"Debug mode:  {debug}")
    print(f"{'=' * 60}")
    print("\nTo start the frontend dev server, run in another terminal:")
    print("  cd resource/frontend && yarn dev")
    print(f"{'=' * 60}\n")

    app = create_app()
    app.run(host="127.0.0.1", port=port, debug=debug)
