"""
Cleanup tasks that have become obsolete
"""

from datetime import timedelta

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

from ocyan.task.models import Task


class Command(BaseCommand):
    help = __doc__

    def add_arguments(self, parser):
        parser.add_argument(
            "weeks",
            type=int,
            help="Number of weeks old the messages should be to be removed",
        )

    def handle(self, *args, **options):
        old = timezone.now() - timedelta(weeks=options["weeks"])
        tasks_to_be_deleted = Task.objects.filter(date_updated__lt=old)
        self.stdout.write(
            self.style.WARNING(
                "%i tasks will be deleted." % tasks_to_be_deleted.count()
            )
        )
        tasks_to_be_deleted.delete()
