import itertools
from datetime import datetime, timedelta
from subprocess import Popen, PIPE

from django.conf import settings
from django.db import connection

import cronjobs
import commonware.log
import waffle

from olympia import amo
from olympia.amo.utils import chunked
from olympia.amo.helpers import user_media_path
from olympia.bandwagon.models import Collection
from olympia.constants.base import VALID_STATUSES
from olympia.devhub.models import ActivityLog
from olympia.lib.es.utils import raise_if_reindex_in_progress
from olympia.stats.models import Contribution

from . import tasks

log = commonware.log.getLogger('z.cron')


@cronjobs.register
def gc(test_result=True):
    """Site-wide garbage collections."""
    def days_ago(days):
        return datetime.today() - timedelta(days=days)

    log.debug('Collecting data to delete')

    logs = (ActivityLog.objects.filter(created__lt=days_ago(90))
            .exclude(action__in=amo.LOG_KEEP).values_list('id', flat=True))

    # Paypal only keeps retrying to verify transactions for up to 3 days. If we
    # still have an unverified transaction after 6 days, we might as well get
    # rid of it.
    contributions_to_delete = (
        Contribution.objects
        .filter(transaction_id__isnull=True, created__lt=days_ago(6))
        .values_list('id', flat=True))

    collections_to_delete = (
        Collection.objects.filter(created__lt=days_ago(2),
                                  type=amo.COLLECTION_ANONYMOUS)
        .values_list('id', flat=True))