from datetime import timedelta

import pytest
from django.utils import timezone

from apps.blog.models import BlogCategory, Post, Tag
from apps.company.models import TeamMember


@pytest.fixture
def posts(db):
    news = BlogCategory.objects.create(name="Actualités", slug="actualites")
    tips = BlogCategory.objects.create(name="Conseils", slug="conseils")
    seo = Tag.objects.create(name="SEO", slug="seo")
    author = TeamMember.objects.create(name="Awa", role="CEO")
    now = timezone.now()

    def make(title, category, days=0, **fields):
        fields = {"excerpt": "Résumé", "published_at": now - timedelta(days=days), **fields}
        return Post.objects.create(title=title, slug=title.lower(), category=category, **fields)

    first = make("Un", news, days=3, author=author, content="<p>" + "mot " * 450 + "</p>")
    first.tags.add(seo)
    make("Deux", tips, days=2)
    make("Trois", news, days=1)
    make("Brouillon", news, is_published=False)
    make("Futur", news, days=-2)
    return first


def test_list_shows_published_posts_newest_first(api, posts):
    data = api.get("/api/v1/posts/").json()
    assert data["count"] == 3
    assert [p["slug"] for p in data["results"]] == ["trois", "deux", "un"]


def test_filter_by_category_and_tag(api, posts):
    by_category = api.get("/api/v1/posts/?category=conseils").json()["results"]
    assert [p["slug"] for p in by_category] == ["deux"]
    assert [p["slug"] for p in api.get("/api/v1/posts/?tag=seo").json()["results"]] == ["un"]


def test_detail_with_author_reading_time_and_related(api, posts):
    data = api.get("/api/v1/posts/un/").json()
    assert data["author"]["name"] == "Awa"
    assert data["reading_time"] == 2
    assert data["tags"] == [{"name": "SEO", "slug": "seo"}]
    assert [p["slug"] for p in data["related"]] == ["trois", "deux"]


def test_drafts_and_scheduled_posts_are_hidden(api, posts):
    assert api.get("/api/v1/posts/brouillon/").status_code == 404
    assert api.get("/api/v1/posts/futur/").status_code == 404


def test_unpublished_author_is_not_shown(api, posts):
    TeamMember.objects.update(is_published=False)
    assert api.get("/api/v1/posts/un/").json()["author"] is None


def test_categories_only_with_visible_posts(api, posts):
    categories = api.get("/api/v1/blog-categories/").json()
    assert categories == [
        {"name": "Actualités", "slug": "actualites", "post_count": 2},
        {"name": "Conseils", "slug": "conseils", "post_count": 1},
    ]


def test_search_and_featured_filters(api, posts):
    Post.objects.filter(slug="deux").update(
        is_featured=True, excerpt="Bien choisir son hébergement"
    )
    assert [p["slug"] for p in api.get("/api/v1/posts/?search=hébergement").json()["results"]] == [
        "deux"
    ]
    assert [p["slug"] for p in api.get("/api/v1/posts/?featured=1").json()["results"]] == ["deux"]
    assert api.get("/api/v1/posts/?search=introuvable").json()["count"] == 0


def test_tags_with_counts_of_visible_posts(api, posts):
    Post.objects.get(slug="brouillon").tags.add(Tag.objects.get(slug="seo"))
    assert api.get("/api/v1/blog-tags/").json() == [{"name": "SEO", "slug": "seo", "post_count": 1}]
