import os
from pathlib import Path
from pydantic_settings import BaseSettings

_HERE = Path(__file__).parent  # always points to local_backend/

# Use AppData on Windows (avoids Controlled Folder Access blocks),
# fall back to local_backend/ on Linux/Mac/Docker
if os.name == "nt":
    _DB_DEFAULT = Path(os.environ.get("LOCALAPPDATA", Path.home() / "AppData" / "Local")) / "pos" / "pos.db"
    _DB_DEFAULT.parent.mkdir(parents=True, exist_ok=True)
else:
    _DB_DEFAULT = _HERE / "pos.db"


class Settings(BaseSettings):
    SITE_ID: str = ""
    SITE_KEY: str = ""
    CLOUD_URL: str = ""
    SECRET_KEY: str = "change-me-generate-a-long-random-string"
    DATABASE_URL: str = f"sqlite:///{_DB_DEFAULT.as_posix()}"
    VERSION: str = "0.3.1"
    CONNECT_SYNC_INTERVAL_SECONDS: int = 30  # how often to poll for pending online orders

    # Break-glass master account — lives only in env vars, never in the DB
    MASTER_USERNAME: str = ""
    MASTER_PASSWORD: str = ""

    model_config = {"env_file": str(_HERE / ".env"), "env_file_encoding": "utf-8"}


settings = Settings()
