from django.core.management.base import BaseCommand

from apps.contacts.models import Contact
from apps.companies.models import Company


NGOS = [
    {
        "name": "WALHI (Wahana Lingkungan Hidup Indonesia)",
        "legal_name": "Yayasan Wahana Lingkungan Hidup Indonesia",
        "industry": "other",
        "city": "Jakarta",
        "country": "Indonesia",
        "website": "https://walhi.or.id",
        "email": "informasi@walhi.or.id",
        "phone": "+62-21-7919-3363",
        "description": "National environmental advocacy NGO.",
    },
    {
        "name": "KontraS",
        "legal_name": "Komisi untuk Orang Hilang dan Korban Tindak Kekerasan",
        "industry": "other",
        "city": "Jakarta",
        "country": "Indonesia",
        "website": "https://kontras.org",
        "email": "kontras_98@kontras.org",
        "phone": "+62-21-392-6983",
        "description": "Human rights and anti-violence advocacy.",
    },
    {
        "name": "LBH Jakarta",
        "legal_name": "Lembaga Bantuan Hukum Jakarta",
        "industry": "other",
        "city": "Jakarta",
        "country": "Indonesia",
        "website": "https://bantuanhukum.or.id",
        "email": "info@bantuanhukum.or.id",
        "phone": "+62-21-314-5518",
        "description": "Legal aid for marginalized groups.",
    },
    {
        "name": "YLKI",
        "legal_name": "Yayasan Lembaga Konsumen Indonesia",
        "industry": "other",
        "city": "Jakarta",
        "country": "Indonesia",
        "website": "https://ylki.or.id",
        "email": "konsumen@ylki.or.id",
        "phone": "+62-21-797-1378",
        "description": "Consumer protection foundation.",
    },
    {
        "name": "Indonesia Corruption Watch",
        "legal_name": "Indonesia Corruption Watch",
        "industry": "other",
        "city": "Jakarta",
        "country": "Indonesia",
        "website": "https://antikorupsi.org",
        "email": "icw@antikorupsi.org",
        "phone": "+62-21-799-3032",
        "description": "Anti-corruption monitoring and research.",
    },
    {
        "name": "YLBHI",
        "legal_name": "Yayasan Lembaga Bantuan Hukum Indonesia",
        "industry": "other",
        "city": "Jakarta",
        "country": "Indonesia",
        "website": "https://ylbhi.or.id",
        "email": "info@ylbhi.or.id",
        "phone": "+62-21-3929840",
        "description": "Federated legal aid network.",
    },
    {
        "name": "Yayasan KEHATI",
        "legal_name": "Yayasan Keanekaragaman Hayati Indonesia",
        "industry": "other",
        "city": "Jakarta",
        "country": "Indonesia",
        "website": "https://kehati.or.id",
        "email": "kehati@kehati.or.id",
        "phone": "+62-21-718-3185",
        "description": "Biodiversity conservation foundation.",
    },
    {
        "name": "Wahid Foundation",
        "legal_name": "Yayasan Wahid",
        "industry": "other",
        "city": "Jakarta",
        "country": "Indonesia",
        "website": "https://wahidfoundation.org",
        "email": "info@wahidfoundation.org",
        "phone": "+62-21-3145671",
        "description": "Pluralism and tolerance advocacy.",
    },
    {
        "name": "Save the Children Indonesia",
        "legal_name": "Yayasan Save the Children Indonesia",
        "industry": "other",
        "city": "Jakarta",
        "country": "Indonesia",
        "website": "https://stcindonesia.org",
        "email": "info@stcindonesia.org",
        "phone": "+62-21-782-4415",
        "description": "Child welfare and rights.",
    },
    {
        "name": "Rumah Cemara",
        "legal_name": "Yayasan Rumah Cemara",
        "industry": "healthcare",
        "city": "Bandung",
        "country": "Indonesia",
        "website": "https://rumahcemara.or.id",
        "email": "info@rumahcemara.or.id",
        "phone": "+62-22-720-4550",
        "description": "HIV/AIDS support and harm reduction.",
    },
    {
        "name": "Yayasan TIFA",
        "legal_name": "Yayasan TIFA",
        "industry": "other",
        "city": "Jakarta",
        "country": "Indonesia",
        "website": "https://tifafoundation.id",
        "email": "info@tifafoundation.id",
        "phone": "+62-21-7884-4946",
        "description": "Open society grantmaking.",
    },
    {
        "name": "Indorelawan",
        "legal_name": "Yayasan Indonesia Berbagi",
        "industry": "other",
        "city": "Jakarta",
        "country": "Indonesia",
        "website": "https://indorelawan.org",
        "email": "halo@indorelawan.org",
        "phone": "+62-21-2952-1212",
        "description": "Volunteer matchmaking platform.",
    },
]


CONTACTS = [
    # WALHI
    {"name": "Nur Hidayati", "position": "Executive Director", "email": "nur.hidayati@walhi.or.id", "phone": "+62-811-100-001", "tags": ["environment", "advocacy"], "company_key": "WALHI (Wahana Lingkungan Hidup Indonesia)", "is_favorite": True},
    {"name": "Bagus Pramudia", "position": "Campaign Manager", "email": "bagus.pramudia@walhi.or.id", "phone": "+62-811-100-002", "tags": ["environment", "climate"], "company_key": "WALHI (Wahana Lingkungan Hidup Indonesia)"},
    {"name": "Dwi Rahmawati", "position": "Policy Researcher", "email": "dwi.rahmawati@walhi.or.id", "phone": "+62-811-100-003", "tags": ["research", "policy"], "company_key": "WALHI (Wahana Lingkungan Hidup Indonesia)"},

    # KontraS
    {"name": "Fatia Maulidiyanti", "position": "Coordinator", "email": "fatia.maulidiyanti@kontras.org", "phone": "+62-811-200-001", "tags": ["human rights", "advocacy"], "company_key": "KontraS", "is_favorite": True},
    {"name": "Andi Muhammad Rezaldy", "position": "Researcher", "email": "andi.rezaldy@kontras.org", "phone": "+62-811-200-002", "tags": ["research", "human rights"], "company_key": "KontraS"},
    {"name": "Tioria Pretty", "position": "Legal Aid Officer", "email": "tioria.pretty@kontras.org", "phone": "+62-811-200-003", "tags": ["legal", "human rights"], "company_key": "KontraS"},

    # LBH Jakarta
    {"name": "Citra Referandum", "position": "Director", "email": "citra.referandum@bantuanhukum.or.id", "phone": "+62-811-300-001", "tags": ["legal", "advocacy"], "company_key": "LBH Jakarta", "is_favorite": True},
    {"name": "Arif Maulana", "position": "Senior Lawyer", "email": "arif.maulana@bantuanhukum.or.id", "phone": "+62-811-300-002", "tags": ["legal"], "company_key": "LBH Jakarta"},
    {"name": "Shaleh Al Ghifari", "position": "Public Defender", "email": "shaleh.alghifari@bantuanhukum.or.id", "phone": "+62-811-300-003", "tags": ["legal", "civil society"], "company_key": "LBH Jakarta"},

    # YLKI
    {"name": "Indah Suksmaningsih", "position": "Chairwoman", "email": "indah.suksma@ylki.or.id", "phone": "+62-811-400-001", "tags": ["consumer", "advocacy"], "company_key": "YLKI"},
    {"name": "Tulus Abadi", "position": "Senior Researcher", "email": "tulus.abadi@ylki.or.id", "phone": "+62-811-400-002", "tags": ["consumer", "research"], "company_key": "YLKI"},
    {"name": "Sri Suparyati", "position": "Programme Officer", "email": "sri.suparyati@ylki.or.id", "phone": "+62-811-400-003", "tags": ["programs"], "company_key": "YLKI"},

    # Indonesia Corruption Watch
    {"name": "Agus Sunaryanto", "position": "Coordinator", "email": "agus.sunaryanto@antikorupsi.org", "phone": "+62-811-500-001", "tags": ["anti-corruption", "advocacy"], "company_key": "Indonesia Corruption Watch", "is_favorite": True},
    {"name": "Egi Primayogha", "position": "Researcher", "email": "egi.primayogha@antikorupsi.org", "phone": "+62-811-500-002", "tags": ["research", "anti-corruption"], "company_key": "Indonesia Corruption Watch"},
    {"name": "Lalola Easter", "position": "Legal Researcher", "email": "lalola.easter@antikorupsi.org", "phone": "+62-811-500-003", "tags": ["legal", "anti-corruption"], "company_key": "Indonesia Corruption Watch"},

    # YLBHI
    {"name": "Muhammad Isnur", "position": "Chairman", "email": "muhammad.isnur@ylbhi.or.id", "phone": "+62-811-600-001", "tags": ["legal", "leadership"], "company_key": "YLBHI"},
    {"name": "Zainal Arifin", "position": "Advocacy Director", "email": "zainal.arifin@ylbhi.or.id", "phone": "+62-811-600-002", "tags": ["advocacy", "legal"], "company_key": "YLBHI"},
    {"name": "Asfinawati", "position": "Senior Lawyer", "email": "asfinawati@ylbhi.or.id", "phone": "+62-811-600-003", "tags": ["legal"], "company_key": "YLBHI"},

    # Yayasan KEHATI
    {"name": "Rony Megawanto", "position": "Programme Director", "email": "rony.megawanto@kehati.or.id", "phone": "+62-811-700-001", "tags": ["biodiversity", "environment"], "company_key": "Yayasan KEHATI"},
    {"name": "Puji Sumedi", "position": "Conservation Manager", "email": "puji.sumedi@kehati.or.id", "phone": "+62-811-700-002", "tags": ["conservation"], "company_key": "Yayasan KEHATI"},
    {"name": "Riki Frindos", "position": "Executive Director", "email": "riki.frindos@kehati.or.id", "phone": "+62-811-700-003", "tags": ["leadership", "environment"], "company_key": "Yayasan KEHATI", "is_favorite": True},

    # Wahid Foundation
    {"name": "Yenny Wahid", "position": "Executive Director", "email": "yenny.wahid@wahidfoundation.org", "phone": "+62-811-800-001", "tags": ["pluralism", "leadership"], "company_key": "Wahid Foundation", "is_favorite": True},
    {"name": "Mujtaba Hamdi", "position": "Programme Manager", "email": "mujtaba.hamdi@wahidfoundation.org", "phone": "+62-811-800-002", "tags": ["programs", "civil society"], "company_key": "Wahid Foundation"},
    {"name": "Alamsyah M. Djafar", "position": "Senior Researcher", "email": "alamsyah.djafar@wahidfoundation.org", "phone": "+62-811-800-003", "tags": ["research", "pluralism"], "company_key": "Wahid Foundation"},

    # Save the Children Indonesia
    {"name": "Selina Patta Sumbung", "position": "Chief Executive", "email": "selina.sumbung@stcindonesia.org", "phone": "+62-811-900-001", "tags": ["children", "leadership"], "company_key": "Save the Children Indonesia"},
    {"name": "Tata Sudrajat", "position": "Child Protection Specialist", "email": "tata.sudrajat@stcindonesia.org", "phone": "+62-811-900-002", "tags": ["children", "protection"], "company_key": "Save the Children Indonesia"},
    {"name": "Wahyu Kuncoro", "position": "Education Manager", "email": "wahyu.kuncoro@stcindonesia.org", "phone": "+62-811-900-003", "tags": ["education", "children"], "company_key": "Save the Children Indonesia"},

    # Rumah Cemara
    {"name": "Ginan Koesmayadi", "position": "Co-founder", "email": "ginan.koesmayadi@rumahcemara.or.id", "phone": "+62-811-110-001", "tags": ["health", "advocacy"], "company_key": "Rumah Cemara"},
    {"name": "Patri Handoyo", "position": "Programme Coordinator", "email": "patri.handoyo@rumahcemara.or.id", "phone": "+62-811-110-002", "tags": ["health", "harm reduction"], "company_key": "Rumah Cemara"},
    {"name": "Yuli Setiawati", "position": "Outreach Worker", "email": "yuli.setiawati@rumahcemara.or.id", "phone": "+62-811-110-003", "tags": ["outreach", "health"], "company_key": "Rumah Cemara"},

    # Yayasan TIFA
    {"name": "Shita Laksmi", "position": "Executive Director", "email": "shita.laksmi@tifafoundation.id", "phone": "+62-811-220-001", "tags": ["grants", "open society"], "company_key": "Yayasan TIFA"},
    {"name": "Darmawan Triwibowo", "position": "Programme Director", "email": "darmawan.triwibowo@tifafoundation.id", "phone": "+62-811-220-002", "tags": ["programs", "civil society"], "company_key": "Yayasan TIFA"},
    {"name": "Lia Marpaung", "position": "Grants Manager", "email": "lia.marpaung@tifafoundation.id", "phone": "+62-811-220-003", "tags": ["grants"], "company_key": "Yayasan TIFA"},

    # Indorelawan
    {"name": "Marsya Anggia", "position": "Co-founder", "email": "marsya.anggia@indorelawan.org", "phone": "+62-811-330-001", "tags": ["volunteer", "youth"], "company_key": "Indorelawan"},
    {"name": "Stefani Hartono", "position": "Community Lead", "email": "stefani.hartono@indorelawan.org", "phone": "+62-811-330-002", "tags": ["community", "volunteer"], "company_key": "Indorelawan"},
    {"name": "Rendy Aditya", "position": "Partnership Officer", "email": "rendy.aditya@indorelawan.org", "phone": "+62-811-330-003", "tags": ["partnership", "volunteer"], "company_key": "Indorelawan"},
]


class Command(BaseCommand):
    help = "Seed Indonesian NGO companies + person contacts"

    def add_arguments(self, parser):
        parser.add_argument("--dry-run", action="store_true", help="Print plan without writing.")

    def handle(self, *args, **options):
        dry = options.get("dry_run", False)

        self.stdout.write(self.style.MIGRATE_HEADING("Indonesian NGO seeder"))
        if dry:
            self.stdout.write(self.style.WARNING("Dry run — no DB writes."))

        # Companies
        company_map = {}
        created_companies = 0
        for data in NGOS:
            if dry:
                exists = Company.objects.filter(name=data["name"]).exists()
                marker = "skip" if exists else "create"
                self.stdout.write(f"  [{marker}] {data['name']}")
                continue
            company, created = Company.objects.get_or_create(
                name=data["name"],
                defaults=data,
            )
            company_map[data["name"]] = company
            if created:
                created_companies += 1
                self.stdout.write(f"  + company {company.name}")

        # Contacts
        created_contacts = 0
        skipped_contacts = 0
        for raw in CONTACTS:
            data = dict(raw)
            company_key = data.pop("company_key")

            if dry:
                exists = Contact.objects.filter(email=data["email"]).exists()
                marker = "skip" if exists else "create"
                self.stdout.write(f"  [{marker}] {data['name']} <{data['email']}>")
                continue

            company = company_map.get(company_key)
            if company is None:
                self.stdout.write(self.style.WARNING(
                    f"  ! missing company '{company_key}' for {data['name']} — skipping"
                ))
                skipped_contacts += 1
                continue

            data["company"] = company
            data.setdefault("is_favorite", False)
            data.setdefault("type", "individual")
            data.setdefault("visibility", "public")

            _, created = Contact.objects.get_or_create(
                email=data["email"],
                defaults=data,
            )
            if created:
                created_contacts += 1
                self.stdout.write(f"  + contact {data['name']}")
            else:
                skipped_contacts += 1

        if dry:
            self.stdout.write(self.style.SUCCESS("Dry run complete."))
            return

        self.stdout.write(self.style.SUCCESS(
            f"Done. companies +{created_companies}, contacts +{created_contacts}, "
            f"contacts skipped {skipped_contacts}."
        ))
