"""App configuration.

Values come from environment variables (set them in cPanel > Setup Python App)
or from a `.env` file next to this one. See .env.example.
"""
import os
from pathlib import Path

BASE_DIR = Path(__file__).resolve().parent


def _load_dotenv(path: Path) -> None:
    if not path.exists():
        return
    for line in path.read_text().splitlines():
        line = line.strip()
        if not line or line.startswith("#") or "=" not in line:
            continue
        key, value = line.split("=", 1)
        os.environ.setdefault(key.strip(), value.strip().strip('"').strip("'"))


_load_dotenv(BASE_DIR / ".env")

SECRET_KEY = os.environ.get("SECRET_KEY", "")
BASE_URL = os.environ.get("BASE_URL", "http://localhost:5000").rstrip("/")
TIMEZONE = os.environ.get("TIMEZONE", "America/Los_Angeles")
DB_PATH = os.environ.get("DB_PATH", str(BASE_DIR / "data" / "planner.db"))

GOOGLE_CLIENT_ID = os.environ.get("GOOGLE_CLIENT_ID", "")
GOOGLE_CLIENT_SECRET = os.environ.get("GOOGLE_CLIENT_SECRET", "")
# Must match an "Authorized redirect URI" in Google Cloud exactly.
GOOGLE_REDIRECT_URI = f"{BASE_URL}/auth/google/callback"

# Cached events older than this are refreshed when the app or widget loads.
SYNC_MAX_AGE_MINUTES = int(os.environ.get("SYNC_MAX_AGE_MINUTES", "10"))
# How many days ahead to keep in the local copy.
SYNC_DAYS_AHEAD = int(os.environ.get("SYNC_DAYS_AHEAD", "21"))
