from django.core.management.base import BaseCommand
from stores.models import StoreRegistry


class Command(BaseCommand):
    help = "Register a store in the ACA Store Registry"

    def add_arguments(self, parser):
        parser.add_argument("--name", required=True)
        parser.add_argument("--slug", required=True)
        parser.add_argument("--url", required=True, help="Base API URL")
        parser.add_argument("--search", default="aca/products/search/")
        parser.add_argument("--detail", default="aca/products/")
        parser.add_argument("--auth", default="none", choices=[c[0] for c in StoreRegistry.AUTH_METHOD_CHOICES])
        parser.add_argument("--key", default="")
        parser.add_argument("--priority", type=int, default=0)

    def handle(self, *args, **options):
        store, created = StoreRegistry.objects.update_or_create(
            slug=options["slug"],
            defaults={
                "name": options["name"],
                "base_api_url": options["url"],
                "search_endpoint": options["search"],
                "detail_endpoint": options["detail"],
                "auth_method": options["auth"],
                "api_key": options["key"],
                "priority": options["priority"],
                "is_active": True,
            },
        )
        action = "Created" if created else "Updated"
        self.stdout.write(self.style.SUCCESS(f"{action} store: {store.name} ({store.slug})"))
