"""Seed the full M&EL domain (idempotent).

Runs the four seeders in dependency order:

1. ``indicators`` — log frame, rows, indicators, targets, data points, RAG
   threshold, outcome/impact assessments, variance snapshots.
2. ``tracking`` — activities, output deliverables, a corrective action.
3. ``reports`` — default ReportTemplate catalogue + a sample published report.
4. ``feedback`` — standing channels, sample submissions, a tracer study,
   course evaluations.

Seeding a published report fires ``mel_report_published`` which the feedback
receiver listens to, so ReportReview channels are created automatically.
"""
from django.core.management.base import BaseCommand

from apps.mel.feedback.seeders.feedback_seeder import seed_feedback
from apps.mel.indicators.seeders.indicators_seeder import seed_indicators
from apps.mel.reports.seeders.reports_seeder import seed_reports
from apps.mel.tracking.seeders.tracking_seeder import seed_tracking


class Command(BaseCommand):
    help = "Seed the M&EL baseline KPI framework and sample operational data (idempotent)."

    def add_arguments(self, parser):
        parser.add_argument(
            "--volume",
            choices=("minimal", "demo", "heavy"),
            default="demo",
            help="Scale activity / report / feedback counts to the tier.",
        )

    def handle(self, *args, **options):
        from apps.core.seeders.volumes import RECORD_COUNTS

        tier = options["volume"]
        counts = RECORD_COUNTS[tier]

        seed_indicators()
        self.stdout.write(self.style.SUCCESS("  ✓ indicators + log frame"))

        # Pass volume through for the sub-seeders that accept it. The
        # signature is best-effort; older versions ignore the kwarg.
        try:
            seed_tracking(activities=counts["activities"])
        except TypeError:
            seed_tracking()
        self.stdout.write(self.style.SUCCESS(f"  ✓ activities + output deliverables (tier={tier})"))

        try:
            seed_reports(reports=counts["reports"])
        except TypeError:
            seed_reports()
        self.stdout.write(self.style.SUCCESS("  ✓ report templates + sample published report"))

        try:
            seed_feedback(submissions=counts["feedback_submissions"])
        except TypeError:
            seed_feedback()
        self.stdout.write(self.style.SUCCESS("  ✓ feedback channels + sample submissions"))

        self.stdout.write(self.style.SUCCESS(f"M&EL seed complete (tier={tier})."))
