import json

import factory
import pytest
import wagtail_factories
from wagtail.models import Page

from associations.models import AssociationIndex, AssociationPage
from events.models import EventIndex, EventPage
from generic.models import GenericPage
from images.models import CustomImage
from news.models import NewsIndex, NewsPage
from studio.models import StudioPage
from venues.models import VenueIndex, VenuePage


class CustomImageFactory(wagtail_factories.ImageFactory):
    class Meta:
        model = CustomImage


class AssociationIndexFactory(wagtail_factories.PageFactory):
    title = factory.Sequence(lambda n: f"Associations {n}")
    lead = "<p>Foreninger og utvalg.</p>"

    class Meta:
        model = AssociationIndex


class AssociationPageFactory(wagtail_factories.PageFactory):
    title = factory.Sequence(lambda n: f"Association {n}")
    excerpt = "Et utdrag."

    class Meta:
        model = AssociationPage


class EventIndexFactory(wagtail_factories.PageFactory):
    title = factory.Sequence(lambda n: f"Events {n}")

    class Meta:
        model = EventIndex


class EventPageFactory(wagtail_factories.PageFactory):
    title = factory.Sequence(lambda n: f"Event {n}")

    class Meta:
        model = EventPage


class GenericPageFactory(wagtail_factories.PageFactory):
    title = factory.Sequence(lambda n: f"Page {n}")
    lead = "<p>Ingress.</p>"

    class Meta:
        model = GenericPage


class StudioPageFactory(wagtail_factories.PageFactory):
    title = factory.Sequence(lambda n: f"Studio {n}")

    class Meta:
        model = StudioPage


class NewsIndexFactory(wagtail_factories.PageFactory):
    title = factory.Sequence(lambda n: f"News {n}")

    class Meta:
        model = NewsIndex


class NewsPageFactory(wagtail_factories.PageFactory):
    title = factory.Sequence(lambda n: f"Article {n}")
    excerpt = "Et utdrag."

    class Meta:
        model = NewsPage


class VenueIndexFactory(wagtail_factories.PageFactory):
    title = factory.Sequence(lambda n: f"Venues {n}")

    class Meta:
        model = VenueIndex


class VenuePageFactory(wagtail_factories.PageFactory):
    title = factory.Sequence(lambda n: f"Venue {n}")

    class Meta:
        model = VenuePage


@pytest.fixture
def root_page(db):
    return Page.objects.get(depth=1)


@pytest.fixture
def home_page(root_page):
    # Wagtail's initial migration creates a default "Welcome" page at depth=2.
    # Reuse it so we don't fight slug collisions across tests.
    return root_page.get_children().first() or root_page.add_child(
        instance=Page(title="Home", slug="home")
    )


@pytest.fixture
def event_index(home_page):
    return EventIndexFactory(parent=home_page)


@pytest.fixture
def news_index(home_page):
    return NewsIndexFactory(parent=home_page)


@pytest.fixture
def association_index(home_page):
    return AssociationIndexFactory(parent=home_page)


@pytest.fixture
def venue(home_page):
    venue_index = VenueIndexFactory(parent=home_page)
    return VenuePageFactory(parent=venue_index)


@pytest.fixture
def graphql_post(client):
    def _post(query, variables=None):
        payload = {"query": query}
        if variables is not None:
            payload["variables"] = variables
        response = client.post(
            "/api/graphql/",
            data=json.dumps(payload),
            content_type="application/json",
        )
        return response, response.json()

    return _post
