from datetime import timedelta

from django.core.management.base import BaseCommand
from django.utils import timezone


class Command(BaseCommand):
    help = "Backdate due_at for testing SLA expiry scenarios (test-only)"

    def add_arguments(self, parser):
        parser.add_argument(
            "--model",
            type=str,
            required=True,
            choices=["complaint", "inquiry", "observation"],
        )
        parser.add_argument("--id", type=str, required=True)
        parser.add_argument("--days-ago", type=int, default=7)

    def handle(self, *args, **options):
        model_name = options["model"]
        entity_id = options["id"]
        days_ago = options["days_ago"]
        past_date = timezone.now() - timedelta(days=days_ago)

        if model_name == "complaint":
            from apps.complaints.models import Complaint

            obj = Complaint.objects.get(id=entity_id)
        elif model_name == "inquiry":
            from apps.complaints.models import Inquiry

            obj = Inquiry.objects.get(id=entity_id)
        elif model_name == "observation":
            from apps.observations.models import Observation

            obj = Observation.objects.get(id=entity_id)

        obj.due_at = past_date
        obj.is_overdue = True
        obj.save(update_fields=["due_at", "is_overdue"])
        self.stdout.write(
            self.style.SUCCESS(
                f"Set due_at={past_date} is_overdue=True for {model_name} {entity_id}"
            )
        )
