from datetime import time, timedelta
import itertools

import pytest
from model_bakery import baker

from aircox import models


@pytest.fixture
def stations():
    return baker.make(models.Station, _quantity=2)


@pytest.fixture
def programs(stations):
    items = list(
        itertools.chain(
            *(
                baker.make(
                    models.Program, station=station, cover=None, _quantity=2
                )
                for station in stations
            )
        )
    )
    for item in items:
        item.save()
    return items


@pytest.fixture
def program(programs):
    return programs[0]


@pytest.fixture
def sched_initials(programs):
    # use concrete class; timezone is provided in order to ensure DST
    items = [
        baker.prepare(
            models.Schedule,
            program=program,
            time=time(16, 00),
            timezone="Europe/Brussels",
        )
        for program in programs
    ]
    models.Schedule.objects.bulk_create(items)
    return items


@pytest.fixture
def sched_reruns(sched_initials):
    # use concrete class
    items = [
        baker.prepare(
            models.Schedule,
            initial=initial,
            program=initial.program,
            date=initial.date,
            time=(initial.start + timedelta(hours=1)).time(),
        )
        for initial in sched_initials
    ]
    models.Schedule.objects.bulk_create(items)
    return items


@pytest.fixture
def schedules(sched_initials, sched_reruns):
    return sched_initials + sched_reruns


@pytest.fixture
def episodes(programs):
    return [
        baker.make(models.Episode, parent=program, cover=None)
        for program in programs
    ]


@pytest.fixture
def episode(episodes):
    return episodes[0]


@pytest.fixture
def podcasts(episodes):
    items = []
    for episode in episodes:
        sounds = baker.prepare(
            models.Sound,
            episode=episode,
            program=episode.program,
            is_public=True,
            _quantity=2,
        )
        for i, sound in enumerate(sounds):
            sound.file = f"test_sound_{episode.pk}_{i}.mp3"
        items += sounds
    return items
