"""
Management command pour tester la configuration email Django
"""
from django.core.management.base import BaseCommand
from django.core.mail import send_mail
from django.conf import settings
from N3wtSchool.mailManager import getConnection, sendMail
import logging

logger = logging.getLogger(__name__)

class Command(BaseCommand):
    help = 'Test de la configuration email'

    def add_arguments(self, parser):
        parser.add_argument('--establishment-id', type=int, help='ID de l\'établissement pour test')
        parser.add_argument('--email', type=str, default='test@example.com', help='Email de destination')

    def handle(self, *args, **options):
        self.stdout.write("=== Test de configuration email ===")

        # Affichage de la configuration
        self.stdout.write(f"EMAIL_HOST: {settings.EMAIL_HOST}")
        self.stdout.write(f"EMAIL_PORT: {settings.EMAIL_PORT}")
        self.stdout.write(f"EMAIL_HOST_USER: {settings.EMAIL_HOST_USER}")
        self.stdout.write(f"EMAIL_HOST_PASSWORD: {settings.EMAIL_HOST_PASSWORD}")
        self.stdout.write(f"EMAIL_USE_TLS: {settings.EMAIL_USE_TLS}")
        self.stdout.write(f"EMAIL_USE_SSL: {settings.EMAIL_USE_SSL}")
        self.stdout.write(f"EMAIL_BACKEND: {settings.EMAIL_BACKEND}")

        # Test 1: Configuration par défaut Django
        self.stdout.write("\n--- Test : Configuration EMAIL par défaut ---")
        try:
            result = send_mail(
                'Test Django Email',
                'Ceci est un test de la configuration email par défaut.',
                settings.EMAIL_HOST_USER,
                [options['email']],
                fail_silently=False,
            )
            self.stdout.write(self.style.SUCCESS(f"✅ Email envoyé avec succès (résultat: {result})"))
        except Exception as e:
            self.stdout.write(self.style.ERROR(f"❌ Erreur: {e}"))