"""
Sends a single test email through whatever EMAIL_* settings are currently
configured — used to verify SMTP credentials on a fresh deployment without
needing shell/interactive access.
"""
from django.core.mail import send_mail
from django.core.management.base import BaseCommand
from django.conf import settings


class Command(BaseCommand):
    help = 'Sends a test email to confirm EMAIL_* settings are working'

    def add_arguments(self, parser):
        parser.add_argument('--to', type=str, required=True, help='Recipient email address')

    def handle(self, *args, **options):
        to = options['to']
        try:
            send_mail(
                subject='Discovr — test email',
                message='If you are reading this, EMAIL_* settings on the live backend are working correctly.',
                from_email=settings.DEFAULT_FROM_EMAIL,
                recipient_list=[to],
                fail_silently=False,
            )
            self.stdout.write(self.style.SUCCESS(f'Sent test email to {to} via {settings.EMAIL_HOST}'))
        except Exception as e:
            self.stdout.write(self.style.ERROR(f'Failed to send: {e}'))
