"""
NAS API Configuration
Environment variables and global settings.
"""

import os
import logging
import requests
import urllib3
from typing import cast
from dotenv import load_dotenv

# Load environment variables from .env.local first, then .env
load_dotenv('.env.local')
load_dotenv()  # Fallback to .env

# Disable SSL warnings for self-signed certificates
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

# Use logger from root (configured in main.py)
logger = logging.getLogger(__name__)

# === NAS Configuration from Environment ===
# NO FALLBACK VALUES - Must be explicitly set in .env.local
_base_url = os.getenv("NAS_BASE_URL")
_username = os.getenv("NAS_USERNAME")
_password = os.getenv("NAS_PASSWORD")
_env_dest_path = os.getenv("NAS_DESTINATION_PATH")

# Validate all required environment variables
missing_vars = []
if not _base_url:
    missing_vars.append("NAS_BASE_URL")
if not _username:
    missing_vars.append("NAS_USERNAME")
if not _password:
    missing_vars.append("NAS_PASSWORD")
if not _env_dest_path:
    missing_vars.append("NAS_DESTINATION_PATH")

if missing_vars:
    error_msg = f"❌ CRITICAL: Missing required environment variables in .env.local: {', '.join(missing_vars)}"
    logger.error(error_msg)
    raise RuntimeError(error_msg)

# After validation, cast to str (guaranteed non-None after error check above)
BASE_URL: str = cast(str, _base_url)
USERNAME: str = cast(str, _username)
PASSWORD: str = cast(str, _password)
DESTINATION_PATH: str = cast(str, _env_dest_path).replace("/", "\\")

print(f"✅ [INIT] NAS Configuration loaded successfully")
print(f"   - Base URL: {BASE_URL}")
print(f"   - Username: {USERNAME}")
print(f"   - Destination Path: {repr(DESTINATION_PATH)}")
logger.debug(
    f"NAS Configuration: BASE_URL={BASE_URL}, USERNAME={USERNAME}, DESTINATION_PATH={DESTINATION_PATH}")

# Global session object
session = requests.Session()
