from django.core.management.base import BaseCommand, CommandError
from wallets.models import Terminal

class Command(BaseCommand):
    help = "Issue or rotate a terminal API key. The plaintext key is shown once."

    def add_arguments(self, parser):
        parser.add_argument("terminal_id")

    def handle(self, *args, **options):
        try:
            terminal = Terminal.objects.get(terminal_id=options["terminal_id"])
        except Terminal.DoesNotExist:
            raise CommandError("Terminal not found.")
        key = terminal.issue_api_key()
        self.stdout.write(self.style.SUCCESS(f"Terminal: {terminal.terminal_id}"))
        self.stdout.write(self.style.WARNING("Store this key securely. It will not be shown again."))
        self.stdout.write(key)
