import os
import django

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'companyos.settings')
django.setup()

from apps.leads.models import Lead
from apps.projects.models import Project
from apps.discussions.models import Discussion

print("Backfilling discussions for existing Leads and Projects...")

# Leads
leads = Lead.objects.all()
created_leads = 0
for lead in leads:
    _, created = Discussion.objects.get_or_create(
        lead=lead, defaults={'board_type': 'lead'}
    )
    if created: created_leads += 1

# Projects
projects = Project.objects.all()
created_projects = 0
for project in projects:
    _, created = Discussion.objects.get_or_create(
        project=project, defaults={'board_type': 'project'}
    )
    if created: created_projects += 1

print(f"Successfully created {created_leads} Lead boards and {created_projects} Project boards.")
