"""Entry point for cPanel / Passenger ("Setup Python App").

cPanel's Python hosting looks for `application` in this file. The Flask
dev server started by `python app.py` is for your own computer; this file
is what runs the exact same app on a real cPanel host, no Docker needed.

Set these in cPanel -> Setup Python App -> Environment variables:

    APP_PASSWORD   required. Without it this dashboard is unprotected and
                   anyone who finds the URL can rewrite your website.
    PUBLIC_URL     e.g. https://tools.yourdomain.com -- so approval links
                   in emails point somewhere reachable from your phone.
    APP_SECRET     optional; a long random string keeps you logged in
                   across restarts.

Two things that matter more than the code:

  1. Put this whole folder OUTSIDE public_html. It holds backups/ (copies
     of your site), reports/, data.db, and your hosting password. Inside
     the web root, all of that is downloadable by anyone who guesses the
     URL. The dashboard warns you on every audit if it detects this.
  2. Serve it over HTTPS -- you're typing a hosting password into it.

With the tool running on the same server as your site, set the Auto-fix
connection to "Local folder" and point it at your site's real path -- no
FTP host, username, or password stored anywhere at all.
"""

# Force UTF-8 everywhere. Windows defaults to cp1252 and some shared hosts
# default to ASCII -- either crashes on a non-ASCII business name, currency
# symbols, accented names, etc. This makes every environment behave like a
# modern Linux box regardless of the host's own locale settings.
import sys as _sys
import os as _os
_os.environ.setdefault("PYTHONUTF8", "1")
for _s in ("stdout", "stderr"):
    _stream = getattr(_sys, _s, None)
    if _stream and hasattr(_stream, "reconfigure"):
        try:
            _stream.reconfigure(encoding="utf-8")
        except Exception:
            pass

import os
import sys
from pathlib import Path

HERE = Path(__file__).parent.resolve()
sys.path.insert(0, str(HERE))

os.environ.setdefault("HOST", "127.0.0.1")  # Passenger fronts us; never bind wide

from app import app as application  # noqa: E402

if not os.environ.get("APP_PASSWORD"):
    sys.stderr.write(
        "\n*** SEO + AI Optimizer: APP_PASSWORD is not set. ***\n"
        "This dashboard can modify your website. Running it on a public\n"
        "server without a password means anyone who finds the URL can\n"
        "change your site. Set APP_PASSWORD in cPanel -> Setup Python App\n"
        "-> Environment variables, then restart the app.\n\n")
