"""
Configuration for platform worker using pydantic-settings.

All service URLs (Odoo, Teams, Logto) are configured here.
The KYC microservice doesn't need to know about these URLs.
Secrets are loaded from Infisical using Machine Identity auth.
"""

import os
from infisical_sdk import InfisicalSDKClient
from pydantic_settings import BaseSettings, SettingsConfigDict


# Load secrets from Infisical before settings initialization
INFISICAL_API_URL = os.environ["INFISICAL_API_URL"]
INFISICAL_CLIENT_ID = os.environ["INFISICAL_CLIENT_ID"]
INFISICAL_CLIENT_SECRET = os.environ["INFISICAL_CLIENT_SECRET"]
INFISICAL_PROJECT_ID = os.environ["INFISICAL_PROJECT_ID"]
INFISICAL_ENV = os.environ.get("INFISICAL_ENV", "prod")

_client = InfisicalSDKClient(host=INFISICAL_API_URL)
_client.auth.universal_auth.login(
    client_id=INFISICAL_CLIENT_ID,
    client_secret=INFISICAL_CLIENT_SECRET,
)

# Fetch secrets from /temporal and /shared
for _secret_path in ["/temporal", "/shared"]:
    _secrets_response = _client.secrets.list_secrets(
        environment_slug=INFISICAL_ENV,
        secret_path=_secret_path,
        project_id=INFISICAL_PROJECT_ID,
        expand_secret_references=True,
        view_secret_value=True,
    )
    for _secret in _secrets_response.secrets:
        os.environ[_secret.secretKey] = _secret.secretValue


class Settings(BaseSettings):
    """Worker configuration loaded from environment variables."""

    model_config = SettingsConfigDict(
        env_file=".env",
        env_file_encoding="utf-8",
        extra="ignore",
    )

    # Temporal connection
    TEMPORAL_HOST: str = "localhost:7233"
    TEMPORAL_NAMESPACE: str = "default"
    TASK_QUEUE: str = "platform-worker"

    # Activity concurrency
    MAX_CONCURRENT_ACTIVITIES: int = 10
    MAX_CONCURRENT_WORKFLOW_TASKS: int = 100

    # Odoo ERP (uses INTERNAL URL for M2M)
    ODOO_INTERNAL_URL: str
    ODOO_KYC_AUTH_USER: str = "kyc_api"
    ODOO_KYC_AUTH_PASS: str = "kyc_api_pass"
    ODOO_KYC_TIMEOUT: float = 30.0

    # Teams service (uses INTERNAL URL for M2M)
    TEAMS_INTERNAL_URL: str
    TEAMS_TIMEOUT: float = 15.0

    # Exchange service (uses INTERNAL URL for M2M)
    EXCHANGE_INTERNAL_URL: str
    EXCHANGE_TIMEOUT: float = 15.0

    # Logto (uses INTERNAL URL for M2M)
    LOGTO_INTERNAL_URL: str
    LOGTO_M2M_APP_ID: str = ""
    LOGTO_M2M_APP_SECRET: str = ""
    LOGTO_M2M_RESOURCE: str = "https://default.logto.app/api"
    LOGTO_TIMEOUT: float = 30.0

    # ArangoDB (loaded from Infisical /shared)
    ARANGODB_INTERNAL_URL: str
    ARANGODB_DATABASE: str
    ARANGODB_PASSWORD: str

    # Prefect (loaded from Infisical /shared)
    PREFECT_API_URL: str

    # TerminusDB
    TERMINUS_DOCUMENT_URL: str = ""
    TERMINUS_BASIC_USER: str = ""
    TERMINUS_BASIC_PASS: str = ""
    TERMINUS_TIMEOUT: float = 15.0

    # SurrealDB status events
    SURREALDB_URL: str = ""
    SURREALDB_NS: str = "optovia"
    SURREALDB_DB: str = "events"
    SURREALDB_USER: str = ""
    SURREALDB_PASS: str = ""
    SURREALDB_TIMEOUT: float = 10.0

    # Novu notifications
    NOVU_API_URL: str = "https://api.novu.co"
    NOVU_API_KEY: str = ""
    NOVU_TRIGGER_ID: str = ""
    NOVU_TIMEOUT: float = 15.0
    INVITE_ACCEPT_URL: str = "https://optovia.ru/invite"


settings = Settings()
