from datetime import date, timedelta

from django.contrib.admin import filters as d_filters
from django.utils.translation import gettext_lazy as _
import pytest

from ..conftest import req_factory
from aircox import models
from aircox.admin import filters


class FakeFilter(d_filters.FieldListFilter):
    def __init__(self, field, request, params, model, model_admin, field_path):
        self.field = field
        self.request = request
        self.params = params
        self.model = model
        self.model_admin = model_admin
        self.field_path = field_path


class DateFieldFilter(filters.DateFieldFilter, FakeFilter):
    pass


today = date.today()
tomorrow = date.today() + timedelta(days=1)


@pytest.fixture
def req():
    return req_factory.get("/test", {"pub_date__gte": today})


@pytest.fixture
def date_filter(req):
    return DateFieldFilter(
        models.Page._meta.get_field("pub_date"),
        req,
        {"pub_date__lte": tomorrow, "other_param": 13},
        models.Page,
        None,
        "pub_date",
    )


class TestDateFieldFilter:
    def test___init__(self, date_filter):
        assert date_filter.date_params == {"pub_date__lte": tomorrow}

        date_filter.links = [(str(link[0]), *list(link[1:])) for link in date_filter.links]
        assert date_filter.links == [
            (str(_("None")), "pub_date__isnull", None, "1"),
            (str(_("Exact")), "pub_date__date", date_filter.input_type),
            (str(_("Since")), "pub_date__gte", date_filter.input_type),
            (str(_("Until")), "pub_date__lte", date_filter.input_type),
        ]
        assert date_filter.query_attrs == {"pub_date__gte": today.strftime("%Y-%m-%d")}
