from django.contrib.auth.models import User
from django.core.management.base import BaseCommand

from core.models import Client


class Command(BaseCommand):
    help = (
        "Reset the owner and client logins. Safe to run any time — it repairs an "
        "account that already exists rather than skipping it, which is what "
        "seed_demo does."
    )

    def add_arguments(self, parser):
        parser.add_argument("--owner-email", default="teniola@teniteno1.com")
        parser.add_argument("--owner-password", default="changeme123")
        parser.add_argument("--client-email", default="chidinma@abc-company.ng")
        parser.add_argument("--client-password", default="clientdemo123")
        parser.add_argument("--owner-only", action="store_true",
                            help="Only touch the owner account.")

    def handle(self, *args, **options):
        owner, _ = User.objects.get_or_create(
            username=options["owner_email"],
            defaults={"email": options["owner_email"],
                      "first_name": "Teniola", "last_name": "Ayodele"},
        )
        owner.email = options["owner_email"]
        owner.is_staff = True
        owner.is_superuser = True
        owner.is_active = True
        owner.set_password(options["owner_password"])
        owner.save()
        self.stdout.write(self.style.SUCCESS(
            f"Owner  {owner.username} / {options['owner_password']}  →  /admin/"))

        if options["owner_only"]:
            return

        user, _ = User.objects.get_or_create(
            username=options["client_email"],
            defaults={"email": options["client_email"],
                      "first_name": "Chidinma", "last_name": "Okafor"},
        )
        user.email = options["client_email"]
        user.is_active = True
        user.set_password(options["client_password"])
        user.save()
        Client.objects.get_or_create(
            user=user, defaults={"company": "ABC Company Ltd", "phone": "+234 802 445 1200"})
        self.stdout.write(self.style.SUCCESS(
            f"Client {user.username} / {options['client_password']}  →  /client/"))

        self.stdout.write("Change both passwords before this goes on a public server.")
