"""
Rest all dan points to 0 at a given date.
"""

from django.core.management.base import BaseCommand
from datetime import date, datetime, time
from mahjong_ranking import models
from django.utils.dateparse import parse_date
from django.utils import timezone

class Command(BaseCommand):
    """ Recalculate all Kyu/Dan Rankings """

    help = "reset every dan player to 1st dan with 0 points."

    def add_arguments(self, parser):
        parser.add_argument('reset_date', type=parse_date)

    def handle(self, *args, **options):
        reset_date = timezone.make_aware(datetime.combine(options.get('reset_date'), time(23, 59, 59)))
        # models.KyuDanRanking.objects.update(until=reset_date, force_recalc=True)
        dan_rankigns = models.KyuDanRanking.objects.filter(dan__isnull=False)
        for ranking in dan_rankigns:
            ranking.dan = 1
            ranking.dan_points = 0
            ranking.legacy_date = reset_date.date()
            ranking.legacy_hanchan_count = ranking.hanchan_count
            ranking.legacy_dan_points = ranking.dan_points
            ranking.legacy_kyu_points = ranking.kyu_points
            ranking.save()


