from django.core.management.base import BaseCommand
from apps.core.models import SiteSetting, Testimonial, FAQ
from apps.properties.models import PropertyCategory, Location
from apps.chatbot.models import ChatbotKnowledge


class Command(BaseCommand):
    help = "Seed demo data for first-time setup"

    def handle(self, *args, **opts):
        site = SiteSetting.get()
        if not site.site_name:
            site.site_name = "TDK Homes"
            site.tagline = "Your Trusted Real Estate Partner"
            site.phone = "+234 (0) 816-553-5147"
            site.email = "info@tdkhomes.ng"
            site.whatsapp_number = "2348165535147"
            site.address = "Abuja, FCT, Nigeria"
            site.hero_title = "Find Your Dream Home in Nigeria"
            site.hero_subtitle = "Premium properties, verified lands, and trusted investments."
            site.about_short = "TDK Homes helps clients buy, sell, and rent verified properties across Nigeria."
            site.clients_count = 500
            site.properties_sold = 200
            site.years_experience = 10
            site.locations_covered = 15
            site.popup_enabled = True
            site.popup_title = "Wait! Don't Miss Out"
            site.popup_subtitle = "Subscribe for exclusive property deals before anyone else."
            site.popup_cta = "Send Me Deals"
            site.chatbot_enabled = True
            site.chatbot_name = "TDK Assistant"
            site.chatbot_greeting = "Hi! How can I help you find your perfect property?"
            site.save()
            self.stdout.write(self.style.SUCCESS("[OK] Site settings"))

        for name in ["Bungalow", "Duplex", "Apartment", "Land", "Commercial", "Mansion"]:
            PropertyCategory.objects.get_or_create(name=name)
        self.stdout.write(self.style.SUCCESS("[OK] Categories"))

        for n, s in [("Lekki", "Lagos"), ("Ikoyi", "Lagos"), ("Victoria Island", "Lagos"),
                     ("Maitama", "FCT"), ("Wuse 2", "FCT"), ("Asokoro", "FCT"),
                     ("Gwarinpa", "FCT"), ("Jabi", "FCT")]:
            Location.objects.get_or_create(name=n, defaults={"state": s})
        self.stdout.write(self.style.SUCCESS("[OK] Locations"))

        faqs = [
            ("How do I schedule a property viewing?",
             "Submit an inquiry on any property page, call us, or use WhatsApp. We arrange viewings within 24-48 hours."),
            ("Are your properties verified?",
             "Yes — every listing is vetted with verified title documents (C of O, Governor's Consent, or Deed of Assignment)."),
            ("Do you offer payment plans?",
             "Many properties offer flexible payment plans tailored to your budget. Contact us for specific terms."),
            ("What documents do I need to buy?",
             "Typically: valid ID, proof of address, and proof of funds. We'll guide you through every step."),
            ("Do you handle rentals as well?",
             "Yes — we have rental, short-let, and lease options across Lagos, Abuja, and other states."),
        ]
        for q, a in faqs:
            FAQ.objects.get_or_create(question=q, defaults={"answer": a})
        self.stdout.write(self.style.SUCCESS("[OK] FAQs"))

        kb = [
            ("Pricing inquiry",
             "Property prices vary by location, type, and size. Browse our listings or share your budget and I'll suggest matching options.",
             "price,cost,how much,budget,afford,expensive,cheap"),
            ("Land for sale",
             "We have verified land listings across Lagos, FCT, and other states — all with proper title documentation.",
             "land,plot,acre,buy land,vacant"),
            ("Mortgage and financing",
             "We work with several mortgage providers in Nigeria. Tell me your budget and I'll connect you with financing options.",
             "mortgage,finance,loan,payment plan,installment"),
            ("Schedule viewing",
             "Sure! Share your name, phone, and the property you're interested in — an agent will reach out within 24 hours.",
             "viewing,inspection,visit,tour,schedule,see"),
            ("Title documents",
             "All our properties come with verified title documents — typically C of O, Governor's Consent, or Deed of Assignment.",
             "title,document,c of o,certificate,papers,legal"),
            ("Rent vs buy",
             "Both rental and purchase options are available. Tell me your needs (location, budget, duration) and I'll guide you.",
             "rent,rental,lease,short let,short-let,shortlet"),
        ]
        for q, a, k in kb:
            ChatbotKnowledge.objects.get_or_create(
                question=q, defaults={"answer": a, "keywords": k, "is_active": True}
            )
        self.stdout.write(self.style.SUCCESS("[OK] Chatbot knowledge base"))

        if not Testimonial.objects.exists():
            for n, r, q in [
                ("Adekunle Olusola", "Property Investor",
                 "TDK Homes helped me secure two investment properties in Lekki. The verification process was thorough and the team was professional throughout."),
                ("Chinwe Okafor", "First-time Buyer",
                 "I was nervous about buying my first property, but the team walked me through every step. Highly recommended."),
                ("Ibrahim Bello", "Business Owner",
                 "Found the perfect commercial space for my business in Wuse 2. Fast, reliable service."),
            ]:
                Testimonial.objects.create(name=n, role=r, quote=q, rating=5)
            self.stdout.write(self.style.SUCCESS("[OK] Testimonials"))

        self.stdout.write(self.style.SUCCESS("\n[DONE] Demo data seeded successfully!"))
        self.stdout.write("Next: python manage.py createsuperuser then python manage.py runserver")
