"""
Test-only helper: print the full champion/manager workflow state for a complaint.
The Playwright spec (Node) shells out to this after each step to assert state
transitions reliably (it cannot read the Django DB directly).

Usage:
  manage.py get_e2e_workflow_state <complaint_id>

Prints key=value lines:
  complaint_status=<status>
  involved_department_id=<uuid|NONE>
  idept_sent=<bool>
  idept_response_submitted=<bool>
  idept_manager_review_status=<pending|approved|rejected|NONE>
  idept_acceptance_status=<pending|acceptable|not_acceptable|NONE>
  explanation_token=<token|NONE>
  explanation_used=<bool>
  investigation_status=<status|NONE>
  investigation_token_staff_<email>=<token|completed>   (0+ lines)
"""

from django.core.management.base import BaseCommand, CommandError

from apps.complaints.models import Complaint, ComplaintExplanation, ComplaintInvolvedDepartment


class Command(BaseCommand):
    help = "Print champion/manager workflow state for a complaint (E2E test helper)."

    def add_arguments(self, parser):
        parser.add_argument("complaint_id", help="Complaint UUID")

    def handle(self, *args, **options):
        cid = options["complaint_id"]
        try:
            complaint = Complaint.objects.get(id=cid)
        except (Complaint.DoesNotExist, ValueError) as exc:
            raise CommandError(f"Complaint {cid} not found: {exc}")

        print(f"complaint_status={complaint.status}")

        idept = ComplaintInvolvedDepartment.objects.filter(complaint=complaint).order_by("-created_at").first()
        if not idept:
            print("involved_department_id=NONE")
            print("idept_sent=False")
            print("idept_response_submitted=False")
            print("idept_manager_review_status=NONE")
            print("idept_acceptance_status=NONE")
        else:
            print(f"involved_department_id={idept.id}")
            print(f"idept_sent={idept.sent}")
            print(f"idept_response_submitted={idept.response_submitted}")
            print(f"idept_manager_review_status={idept.manager_review_status or 'NONE'}")
            print(f"idept_acceptance_status={idept.acceptance_status or 'NONE'}")

        expl = ComplaintExplanation.objects.filter(complaint=complaint).order_by("-created_at").first()
        print(f"explanation_token={expl.token if expl else 'NONE'}")
        print(f"explanation_used={expl.is_used if expl else 'NONE'}")

        try:
            from apps.complaints.models import ChampionInvestigation, InvestigationResponse
        except ImportError:
            print("investigation_status=UNAVAILABLE")
            return
        inv = ChampionInvestigation.objects.filter(complaint=complaint).order_by("-created_at").first()
        print(f"investigation_status={inv.status if inv else 'NONE'}")
        if inv:
            for ir in InvestigationResponse.objects.filter(investigation=inv):
                email = ir.staff.user.email if ir.staff and ir.staff.user else (ir.staff_id or "?")
                print(f"investigation_token_staff_{email}={'completed' if ir.is_completed else ir.token}")
