"""
License server config — shared between server, keygen, and client validator.
"""

from __future__ import annotations

import os
from pathlib import Path

# Server identity
PRODUCT_ID = "gold_autotrade"
PRODUCT_NAME = "Gold AutoTrade"
LICENSE_VERSION = 1

# Secret for HMAC signing.
#
#   Production (server):  set GAT_LICENSE_SECRET env var to a strong hex value.
#                         Generate: python3 -c 'import secrets; print(secrets.token_hex(32))'
#
#   Trial   (client zip): uses the hardcoded fallback below.
#                         This is safe because trial licenses are self-signed
#                         by the client — no server trust is involved.
#                         The fallback MUST differ from the production secret.
#
#   Paid    (client):     activation sets GAT_LICENSE_SECRET on the client machine
#                         so the validator can verify the server's HMAC.
#
_TRIAL_SECRET = "gAt_Tr1al_v1_hardcoded_self_signed_only_N0T_4_Production"
_SERVER_SECRET = os.environ.get("GAT_LICENSE_SECRET", "")
if _SERVER_SECRET:
    SIGNING_SECRET = _SERVER_SECRET
else:
    # Client side (no env var) — use trial secret for self-signed trial licenses.
    # Paid activation will persist the server's secret via client_secret file.
    SIGNING_SECRET = _TRIAL_SECRET

# License tiers
TIERS = {
    "trial": {"max_days": 14, "max_machines": 1, "features": ["basic"]},
    "pro": {"max_days": 365, "max_machines": 2, "features": ["basic", "ai_signals", "auto_trade"]},
    "enterprise": {"max_days": 365, "max_machines": 10, "features": ["basic", "ai_signals", "auto_trade", "multi_tenant", "api_access"]},
}

# License key format: GAT-XXXX-XXXX-XXXX-XXXX (uppercase alphanum, 4 groups of 4)
KEY_PREFIX = "GAT"
KEY_GROUP_LENGTH = 4
KEY_GROUPS = 4

# Server paths
DATA_DIR = Path(__file__).resolve().parent / "data"
DB_PATH = DATA_DIR / "licenses.db"

# Client-side paths
def _default_license_dir() -> Path:
    """Cross-platform license storage directory."""
    if os.name == "nt":
        base = os.environ.get("APPDATA", os.path.expanduser("~"))
    elif os.sys.platform == "darwin":
        base = os.path.expanduser("~/Library/Application Support")
    else:
        base = os.environ.get("XDG_DATA_HOME", os.path.expanduser("~/.local/share"))
    return Path(base) / "GoldAutoTrade" / "license"

CLIENT_LICENSE_DIR = _default_license_dir()
CLIENT_LICENSE_FILE = CLIENT_LICENSE_DIR / "license.json"
CLIENT_SECRET_FILE = CLIENT_LICENSE_DIR / ".signing_secret"
CLIENT_PUBLIC_KEY_FILE = CLIENT_LICENSE_DIR / "public_key.pem"
