from django.core.management.base import BaseCommand

from apps.notifications.services import NotificationService


class Command(BaseCommand):
    help = "Send a test SMS to a phone number"

    def add_arguments(self, parser):
        parser.add_argument("phone", type=str, help="Phone number in international format (e.g. 966501234567)")
        parser.add_argument("--message", type=str, default="Test SMS from PX360", help="Custom message text")

    def handle(self, *args, **options):
        phone = options["phone"]
        message = options["message"]

        self.stdout.write(f"Sending SMS to {phone}...")
        self.stdout.write(f"Message: {message}")
        self.stdout.write("")

        log = NotificationService.send_sms(phone, message)

        self.stdout.write(f"Status: {log.status}")
        self.stdout.write(f"Provider: {log.provider}")
        if log.error:
            self.stdout.write(self.style.ERROR(f"Error: {log.error}"))
        if log.provider_response:
            self.stdout.write(f"Provider Response: {log.provider_response}")

        if log.status == "sent":
            self.stdout.write(self.style.SUCCESS(f"SMS sent successfully to {phone}"))
        elif log.status == "failed":
            self.stdout.write(self.style.ERROR(f"SMS failed for {phone}"))
        else:
            self.stdout.write(self.style.WARNING(f"SMS status: {log.status} for {phone}"))
