#!/usr/bin/env python3
"""
Check KPI Analysis configuration
"""

import sys
from pathlib import Path

# Add current directory to path
sys.path.insert(0, str(Path(__file__).parent))

from config.settings import settings

print("=" * 70)
print("KPI Analysis Dashboard - Configuration Check")
print("=" * 70)

print("\n📋 Application Settings")
print("-" * 70)
print(f"  App Name: {settings.app_name}")
print(f"  Version: {settings.app_version}")
print(f"  Debug Mode: {settings.debug}")
print(f"  Host: {settings.host}")
print(f"  Port: {settings.port}")
print(f"  Secret Key: {'*' * 20} (configured: {bool(settings.secret_key)})")

print("\n🔐 Authentication Settings")
print("-" * 70)
print(f"  LDAP Server: {settings.ldap_server or 'Not configured'}")
print(f"  LDAP Port: {settings.ldap_port}")
print(f"  LDAP Use SSL: {settings.ldap_use_ssl}")
print(f"  LDAP Base DN: {settings.ldap_base_dn or 'Not configured'}")
print(f"  LDAP Group DN: {settings.ldap_kpi_group_dn or 'Not configured'}")

print("\n🔓 Fallback Authentication")
print("-" * 70)
print(f"  Enabled: {settings.enable_fallback_auth}")
if settings.enable_fallback_auth:
    print(f"  Username: {settings.fallback_admin_username}")
    print(f"  Password: {'*' * len(settings.fallback_admin_password)}")
    print(f"  Email: {settings.fallback_admin_email}")
    print(f"  Role: {settings.fallback_admin_role}")
else:
    print(f"  ⚠️  Fallback authentication is disabled")

print("\n☁️  Nextcloud Settings")
print("-" * 70)
print(f"  Base URL: {settings.nextcloud_base_url}")
print(f"  OAuth Client ID: {settings.nextcloud_oauth_client_id[:20] + '...' if settings.nextcloud_oauth_client_id else 'Not configured'}")
print(f"  OAuth Client Secret: {'*' * 20 if settings.nextcloud_oauth_client_secret else 'Not configured'}")
print(f"  KPI Folder: {settings.nextcloud_kpi_folder}")
print(f"  Username: {settings.nextcloud_username or 'Not configured'}")

print("\n🤖 OpenAI Settings")
print("-" * 70)
print(f"  API Key: {'*' * 20 if settings.openai_api_key else 'Not configured'}")
print(f"  Model: {settings.openai_model}")
print(f"  Max Tokens: {settings.openai_max_tokens}")
print(f"  Temperature: {settings.openai_temperature}")

print("\n💾 Database Settings")
print("-" * 70)
print(f"  Database URL: {settings.database_url}")

print("\n📁 Directory Settings")
print("-" * 70)
print(f"  Data Directory: {settings.data_directory}")
print(f"  Upload Directory: {settings.upload_directory}")
print(f"  Reports Directory: {settings.reports_directory}")

# Check if directories exist
import os
data_exists = os.path.exists(settings.data_directory)
upload_exists = os.path.exists(settings.upload_directory)
reports_exists = os.path.exists(settings.reports_directory)

print(f"\n  Directory Status:")
print(f"    Data: {'✅ Exists' if data_exists else '❌ Missing'}")
print(f"    Upload: {'✅ Exists' if upload_exists else '❌ Missing'}")
print(f"    Reports: {'✅ Exists' if reports_exists else '❌ Missing'}")

print("\n🔒 Security Settings")
print("-" * 70)
print(f"  Session Timeout: {settings.session_timeout_minutes} minutes")
print(f"  Max File Size: {settings.max_file_size_mb} MB")
print(f"  Allowed Extensions: {', '.join(settings.allowed_file_extensions)}")

print("\n🌐 CORS Settings")
print("-" * 70)
print(f"  Allowed Origins:")
for origin in settings.effective_cors_origins:
    print(f"    - {origin}")

print("\n📧 Email Settings")
print("-" * 70)
print(f"  SMTP Server: {settings.smtp_server or 'Not configured'}")
print(f"  SMTP Port: {settings.smtp_port}")
print(f"  SMTP Username: {settings.smtp_username or 'Not configured'}")
print(f"  Email From: {settings.email_from or 'Not configured'}")

print("\n📝 Logging Settings")
print("-" * 70)
print(f"  Log Level: {settings.log_level}")
print(f"  Log File: {settings.log_file}")

# Check if log directory exists
log_dir = os.path.dirname(settings.log_file)
log_dir_exists = os.path.exists(log_dir)
print(f"  Log Directory: {'✅ Exists' if log_dir_exists else '❌ Missing'}")

print("\n" + "=" * 70)
print("Configuration Summary")
print("=" * 70)

# Determine authentication status
auth_status = []
if settings.ldap_server:
    auth_status.append("LDAP configured")
if settings.enable_fallback_auth:
    auth_status.append("Fallback enabled")

if not auth_status:
    print("❌ No authentication method configured!")
    print("   Please configure LDAP or enable fallback authentication")
else:
    print(f"✅ Authentication: {', '.join(auth_status)}")

# Check critical settings
critical_ok = True

if not settings.secret_key or settings.secret_key == "your-secret-key-here-change-in-production":
    print("⚠️  WARNING: Using default SECRET_KEY - change this in production!")
    critical_ok = False

if not data_exists or not upload_exists or not reports_exists:
    print("⚠️  WARNING: Some required directories are missing")
    critical_ok = False

if critical_ok:
    print("✅ All critical settings are configured")

print("\n" + "=" * 70)
print("Ready to start the application!")
print("Run: python run.py")
print("=" * 70)
