"""Seed regular (non-project-linked) donor companies.

Project donors are auto-created by the ProjectFund pre_save signal. Regular
donors live in the phonebook without being tied to a specific ProjectFund.

Usage:
    uv run python manage.py seed_regular_donors [--force]
"""

import random

from django.core.management.base import BaseCommand

from apps.companies.models import Company


REGULAR_DONORS = [
    {"name": "Astra International Foundation", "industry": "Corporate", "city": "Jakarta", "country": "Indonesia",
     "website": "https://www.astra.co.id", "description": "Corporate philanthropy arm of Astra Group."},
    {"name": "Bakti Pertamina", "industry": "Corporate", "city": "Jakarta", "country": "Indonesia",
     "description": "Pertamina CSR foundation."},
    {"name": "Dompet Dhuafa", "industry": "Foundation", "city": "Jakarta", "country": "Indonesia",
     "website": "https://www.dompetdhuafa.org", "description": "Indonesian zakat-based humanitarian foundation."},
    {"name": "Yayasan Tahija", "industry": "Foundation", "city": "Jakarta", "country": "Indonesia",
     "description": "Private family foundation supporting health and conservation."},
    {"name": "Yayasan William & Lily", "industry": "Foundation", "city": "Jakarta", "country": "Indonesia",
     "description": "Private foundation supporting education and arts."},
    {"name": "Kitabisa Donor Network", "industry": "Individual", "city": "Jakarta", "country": "Indonesia",
     "website": "https://kitabisa.com",
     "description": "Aggregated individual donors via crowdfunding platform."},
    {"name": "Indofood CBP Sukses Makmur", "industry": "Corporate", "city": "Jakarta", "country": "Indonesia",
     "description": "Corporate sponsor for events and outreach."},
    {"name": "Bank Mandiri CSR", "industry": "Finance", "city": "Jakarta", "country": "Indonesia",
     "description": "Bank Mandiri corporate social responsibility unit."},
    {"name": "BCA Foundation", "industry": "Finance", "city": "Jakarta", "country": "Indonesia",
     "description": "BCA charitable foundation."},
    {"name": "MAYAPADA Healthcare Group", "industry": "Corporate", "city": "Jakarta", "country": "Indonesia",
     "description": "Occasional sponsor for health-related initiatives."},
    {"name": "Goethe-Institut Indonesien", "industry": "Government", "city": "Jakarta", "country": "Germany",
     "website": "https://www.goethe.de/ins/id",
     "description": "German cultural institute, occasional event co-funder."},
    {"name": "Embassy of Japan", "industry": "Government", "city": "Jakarta", "country": "Japan",
     "description": "Cultural and grassroots grants (GGP)."},
    {"name": "Embassy of Canada", "industry": "Government", "city": "Jakarta", "country": "Canada",
     "description": "Canada Fund for Local Initiatives donor."},
    {"name": "Anonymous Individual Donors", "industry": "Individual", "city": "—", "country": "—",
     "description": "Pooled record for one-off individual contributions."},
    {"name": "Yayasan Sayangi Tunas Cilik", "industry": "Foundation", "city": "Jakarta", "country": "Indonesia",
     "description": "Local NGO partner that also contributes funding."},
]


class Command(BaseCommand):
    help = "Seed regular donor companies (donor_category='regular')"

    def add_arguments(self, parser):
        parser.add_argument(
            "--force", action="store_true",
            help="Delete existing regular donors before seeding",
        )

    def handle(self, *args, **opts):
        force = opts["force"]
        rng = random.Random(7)

        if force:
            removed, _ = Company.objects.filter(
                company_type="donor", donor_category="regular",
            ).delete()
            self.stdout.write(f"Removed {removed} regular donors (--force).")

        created = 0
        skipped = 0
        for entry in REGULAR_DONORS:
            existing = Company.objects.filter(name__iexact=entry["name"]).first()
            if existing:
                # Promote existing org to donor / regular if not already.
                changed = False
                if existing.company_type != "donor":
                    existing.company_type = "donor"
                    changed = True
                if existing.donor_category != "regular":
                    # Don't downgrade project donors to regular.
                    if existing.donor_category != "project":
                        existing.donor_category = "regular"
                        changed = True
                if changed:
                    existing.save(update_fields=["company_type", "donor_category", "updated_at"])
                skipped += 1
                continue

            Company.objects.create(
                name=entry["name"],
                company_type="donor",
                donor_category="regular",
                industry=entry.get("industry", ""),
                website=entry.get("website", ""),
                city=entry.get("city", ""),
                country=entry.get("country", ""),
                description=entry.get("description", ""),
                phone=rng.choice(["", "+62 21 5520 100", "+62 21 7918 9000", "+62 21 2300 600"]),
                email=f"contact@{entry['name'].lower().replace(' ', '').replace('&', '').replace('—', '')[:25]}.id",
            )
            created += 1

        self.stdout.write(self.style.SUCCESS(
            f"Regular donors: created={created}, skipped={skipped}"
        ))
