"""
Django settings for wallet_server project.

Generated by 'django-admin startproject' using Django 5.2.8.

For more information on this file, see
https://docs.djangoproject.com/en/5.2/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/5.2/ref/settings/

BRC-104 Authentication and Payment middleware configuration is at the bottom.
Uses py-middleware package for BSV authentication.
"""

import sys
from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

# Add local py-sdk and py-middleware to path (for development)
# This ensures we use local versions instead of installed packages
# Try multiple locations where these packages might be
lib_root = BASE_DIR.parent.parent.parent  # Relative to storage_server_example: ../../.. (py-lib root)
home_py_lib = Path.home() / "py-lib"

# Try to find py-sdk and py-middleware in common locations
possible_sdk_paths = [
    lib_root / "py-sdk",  # Relative to storage_server_example: ../../../py-sdk
    home_py_lib / "py-sdk",  # Common development location
]

possible_middleware_paths = [
    lib_root / "py-middleware",  # Relative to storage_server_example: ../../../py-middleware
    home_py_lib / "py-middleware",  # Common development location
]

# Find and add py-sdk to path
found_sdk = None
for path in possible_sdk_paths:
    if path.exists() and (path / "bsv").exists():
        found_sdk = path
        sys.path.insert(0, str(path))
        print(f"[SETTINGS] Using local py-sdk from: {path}")
        break

# Find and add py-middleware to path
found_middleware = None
for path in possible_middleware_paths:
    examples_path = path / "examples" / "django_example"
    if examples_path.exists():
        found_middleware = path
        sys.path.insert(0, str(path))
        print(f"[SETTINGS] Using local py-middleware from: {path}")
        break

# Warn if local dependencies not found (will fall back to installed packages)
if not found_sdk:
    print("[SETTINGS] WARNING: Local py-sdk not found, will use installed package")
if not found_middleware:
    print("[SETTINGS] WARNING: Local py-middleware not found, will use installed package")
    # Also check if installed as editable (check .pth files or dist-info)
    try:
        import bsv_middleware

        middleware_file = Path(bsv_middleware.__file__)
        # For editable installs, the file might be a link or in a src directory
        # Try going up from site-packages to find the source
        if "site-packages" in str(middleware_file):
            # Try to find the source directory
            site_packages = middleware_file.parent.parent
            # Check if there's a .pth file pointing to the source
            for pth_file in site_packages.glob("*.pth"):
                try:
                    with open(pth_file) as f:
                        for line in f:
                            line = line.strip()
                            if line and not line.startswith("import"):
                                pth_path = Path(line)
                                if pth_path.exists() and (pth_path / "examples" / "django_example").exists():
                                    sys.path.insert(0, str(pth_path))
                                    print(f"[SETTINGS] Using editable py-middleware from: {pth_path}")
                                    found_middleware = pth_path
                                    break
                except:
                    pass
    except ImportError:
        pass


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/5.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = "django-insecure-yavde1#iud62ylvl=-twwg4!(2fyyfpkbea142bbg2ml67vs8l"

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

# Allow localhost for development
ALLOWED_HOSTS = ["localhost", "127.0.0.1", "0.0.0.0"]


# Application definition

INSTALLED_APPS = [
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    # Project apps
    "wallet_app",
    # Third-party apps
    "rest_framework",
]

# =============================================================================
# Middleware Configuration
# =============================================================================
# Order matters! CORS must be first, then security, then auth.
#
# Reference architecture (matching Go/TypeScript implementations):
#   1. Django Security - Standard Django security
#   2. BSV Auth Middleware (py-middleware) - BRC-104 authentication
#   3. BSV Payment Middleware (py-middleware) - Monetization (optional)
#   4. Application handlers
#
# To enable BSV authentication, uncomment the middleware lines below
# and configure BSV_MIDDLEWARE settings.
# =============================================================================

MIDDLEWARE = [
    # Django standard middleware
    "django.middleware.security.SecurityMiddleware",
    "django.contrib.sessions.middleware.SessionMiddleware",
    "django.middleware.common.CommonMiddleware",
    "django.middleware.csrf.CsrfViewMiddleware",
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    "django.contrib.messages.middleware.MessageMiddleware",
    "django.middleware.clickjacking.XFrameOptionsMiddleware",
    # ==========================================================================
    # BSV Middleware (py-middleware package)
    # BRC-104 authentication ENABLED (signature formats now compatible)
    # Production middleware from bsv_middleware.django
    # ==========================================================================
    "bsv_middleware.django.auth_middleware.BSVAuthMiddleware",
    # 'bsv_middleware.django.payment_middleware_complete.BSVPaymentMiddleware',  # Uncomment for payment
]

ROOT_URLCONF = "wallet_server.urls"

TEMPLATES = [
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        "DIRS": [],
        "APP_DIRS": True,
        "OPTIONS": {
            "context_processors": [
                "django.template.context_processors.request",
                "django.contrib.auth.context_processors.auth",
                "django.contrib.messages.context_processors.messages",
            ],
        },
    },
]

WSGI_APPLICATION = "wallet_server.wsgi.application"


# Database
# https://docs.djangoproject.com/en/5.2/ref/settings/#databases

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.sqlite3",
        "NAME": BASE_DIR / "django_admin.sqlite3",
    }
}


# Password validation
# https://docs.djangoproject.com/en/5.2/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        "NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
    },
    {
        "NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
    },
    {
        "NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",
    },
    {
        "NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
    },
]


# Internationalization
# https://docs.djangoproject.com/en/5.2/topics/i18n/

LANGUAGE_CODE = "en-us"

TIME_ZONE = "UTC"

USE_I18N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/5.2/howto/static-files/

STATIC_URL = "static/"

# Default primary key field type
# https://docs.djangoproject.com/en/5.2/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

# REST Framework configuration
REST_FRAMEWORK = {
    "DEFAULT_RENDERER_CLASSES": [
        "rest_framework.renderers.JSONRenderer",
    ],
    "DEFAULT_PARSER_CLASSES": [
        "rest_framework.parsers.JSONParser",
    ],
}


# =============================================================================
# BSV Middleware Configuration (py-middleware package)
# =============================================================================
# Reference: py-middleware/examples/django_example/adapter/
#            go-wallet-toolbox/pkg/storage/server.go
#            go-bsv-middleware/pkg/middleware/
# =============================================================================

# Server wallet is initialized lazily when the middleware loads
# This avoids circular imports during Django startup
_server_wallet_instance = None


def get_bsv_wallet():
    """Get or create server wallet for BSV middleware."""
    global _server_wallet_instance
    if _server_wallet_instance is None:
        from wallet_app.services import get_server_wallet

        _server_wallet_instance = get_server_wallet()
    return _server_wallet_instance


# Note: BSV_MIDDLEWARE['WALLET'] will be set by the middleware on first access
# The middleware checks for callable and calls it if needed
BSV_MIDDLEWARE = {
    # Required: Wallet instance for authentication
    # Uses get_server_wallet() from wallet_app.services (lazy loaded)
    "WALLET": None,  # Will be set below after definition
    "WALLET_GETTER": get_bsv_wallet,  # Callable for lazy initialization
    # Allow unauthenticated requests (for development/testing)
    # Set to False in production to require BRC-104 authentication
    "ALLOW_UNAUTHENTICATED": False,
    # Certificate requirements for mutual authentication
    "CERTIFICATE_REQUESTS": None,
    # Callback when certificates are received
    "ON_CERTIFICATES_RECEIVED": None,
    # Custom session manager (optional)
    "SESSION_MANAGER": None,
    # Logging configuration
    "LOG_LEVEL": "debug",  # 'debug', 'info', 'warn', 'error'
}

# =============================================================================
# Django Logging Configuration
# =============================================================================
LOGGING = {
    "version": 1,
    "disable_existing_loggers": False,
    "formatters": {
        "verbose": {
            "format": "{levelname} {asctime} {module} {message}",
            "style": "{",
        },
        "simple": {
            "format": "{levelname} {message}",
            "style": "{",
        },
    },
    "handlers": {
        "console": {
            "class": "logging.StreamHandler",
            "formatter": "verbose",
        },
        "file": {
            "class": "logging.FileHandler",
            "filename": "/tmp/wallet_server_debug.log",
            "formatter": "verbose",
        },
    },
    "root": {
        "handlers": ["console", "file"],
        "level": "INFO",
    },
    "loggers": {
        "wallet_app": {
            "handlers": ["console", "file"],
            "level": "DEBUG",
            "propagate": False,
        },
        "bsv_wallet_toolbox.storage.provider": {
            "handlers": ["console", "file"],
            "level": "DEBUG",
            "propagate": False,
        },
        "django": {
            "handlers": ["console"],
            "level": "INFO",
            "propagate": False,
        },
    },
}


# =============================================================================
# CORS Configuration
# =============================================================================
# For cross-origin requests, install django-cors-headers:
#   pip install django-cors-headers
#
# Then add to INSTALLED_APPS and MIDDLEWARE:
#   INSTALLED_APPS = [..., 'corsheaders', ...]
#   MIDDLEWARE = ['corsheaders.middleware.CorsMiddleware', ...]
#
# CORS_ALLOWED_ORIGINS = [
#     "http://localhost:3000",
#     "http://127.0.0.1:3000",
# ]
# =============================================================================
