from datetime import timedelta

import pytest

from django.core.exceptions import ValidationError

# we use Schedule as concrete class (Rerun is abstract)
from aircox.models import Schedule


class TestRerunQuerySet:
    @pytest.mark.django_db
    def test_station_by_obj(self, stations, schedules):
        for station in stations:
            queryset = Schedule.objects.station(station).distinct().values_list("program__station", flat=True)
            assert queryset.count() == 1
            assert queryset.first() == station.pk

    @pytest.mark.django_db
    def test_station_by_id(self, stations, schedules):
        for station in stations:
            queryset = Schedule.objects.station(id=station.pk).distinct().values_list("program__station", flat=True)
            assert queryset.count() == 1
            assert queryset.first() == station.pk

    @pytest.mark.django_db
    def test_program_by_obj(self, programs, schedules):
        for program in programs:
            queryset = Schedule.objects.program(program).distinct().values_list("program", flat=True)
            assert queryset.count() == 1
            assert queryset.first() == program.pk

    @pytest.mark.django_db
    def test_program_by_id(self, programs, schedules):
        for program in programs:
            queryset = Schedule.objects.program(id=program.pk).distinct().values_list("program", flat=True)
            assert queryset.count() == 1
            assert queryset.first() == program.pk

    @pytest.mark.django_db
    def test_rerun(self, schedules):
        queryset = Schedule.objects.rerun().values_list("initial", flat=True)
        assert None not in queryset

    @pytest.mark.django_db
    def test_initial(self, schedules):
        queryset = Schedule.objects.initial().distinct().values_list("initial", flat=True)
        assert queryset.count() == 1
        assert queryset.first() is None


class TestRerun:
    @pytest.mark.django_db
    def test_is_initial_true(self, sched_initials):
        assert all(r.is_initial for r in sched_initials)

    @pytest.mark.django_db
    def test_is_initial_false(self, sched_reruns):
        assert all(not r.is_initial for r in sched_reruns)

    @pytest.mark.django_db
    def test_is_rerun_true(self, sched_reruns):
        assert all(r.is_rerun for r in sched_reruns)

    @pytest.mark.django_db
    def test_is_rerun_false(self, sched_initials):
        assert all(not r.is_rerun for r in sched_initials)

    @pytest.mark.django_db
    def test_get_initial_of_initials(self, sched_initials):
        assert all(r.get_initial() is r for r in sched_initials)

    @pytest.mark.django_db
    def test_get_initial_of_reruns(self, sched_reruns):
        assert all(r.get_initial() is r.initial for r in sched_reruns)

    @pytest.mark.django_db
    def test_clean_success(self, sched_reruns):
        for rerun in sched_reruns:
            rerun.clean()

    @pytest.mark.django_db
    def test_clean_fails(self, sched_reruns):
        for rerun in sched_reruns:
            rerun.time = (rerun.initial.start - timedelta(hours=2)).time()
            with pytest.raises(ValidationError):
                rerun.clean()

    @pytest.mark.django_db
    def test_save_rerun(self, sched_reruns):
        for rerun in sched_reruns:
            rerun.program = None
            rerun.save_rerun()
            assert rerun.program == rerun.initial.program

    # TODO: save()
    # save_initial is empty, thus not tested
