from datetime import datetime, timezone

from django.test import TestCase
from django.urls import reverse

from newsreader.accounts.tests.factories import UserFactory
from newsreader.news.collection.tests.factories import FeedFactory
from newsreader.news.core.tests.factories import CategoryFactory, FeedPostFactory


class PostListViewTestCase(TestCase):
    def setUp(self):
        self.user = UserFactory(is_staff=True, password="test")
        self.client.force_login(self.user)

    def test_simple(self):
        rule = FeedFactory(user=self.user, category=CategoryFactory(user=self.user))
        FeedPostFactory.create_batch(size=3, rule=rule)

        response = self.client.get(reverse("api:news:core:posts-list"))
        data = response.json()

        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(data["results"]), 3)

    def test_ordering(self):
        rule = FeedFactory(user=self.user, category=CategoryFactory(user=self.user))

        posts = [
            FeedPostFactory(
                title="I'm the first post",
                rule=rule,
                publication_date=datetime(2019, 5, 20, 16, 7, 38, tzinfo=timezone.utc),
            ),
            FeedPostFactory(
                title="I'm the second post",
                rule=rule,
                publication_date=datetime(2019, 5, 20, 16, 7, 37, tzinfo=timezone.utc),
            ),
            FeedPostFactory(
                title="I'm the third post",
                rule=rule,
                publication_date=datetime(2019, 5, 20, 16, 7, 36, tzinfo=timezone.utc),
            ),
        ]

        response = self.client.get(reverse("api:news:core:posts-list"))
        data = response.json()

        self.assertEqual(response.status_code, 200)

        for index, post in enumerate(posts, start=0):
            with self.subTest(post=post):
                self.assertEqual(data["results"][index]["id"], post.pk)

    def test_posts(self):
        rule = FeedFactory(user=self.user, category=CategoryFactory(user=self.user))

        FeedPostFactory.create_batch(size=20, rule=rule, read=False)
        FeedPostFactory.create_batch(size=10, rule=rule, read=True)

        response = self.client.get(reverse("api:news:core:posts-list"))

        data = response.json()
        posts = data["results"]

        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(data["results"]), 20)

        for post in posts:
            with self.subTest(post=post):
                self.assertEqual(post["read"], False)

    def test_saved_posts(self):
        rule = FeedFactory(user=self.user, category=CategoryFactory(user=self.user))

        FeedPostFactory.create_batch(size=20, rule=rule, saved=False)
        FeedPostFactory.create_batch(size=10, rule=rule, saved=True)

        response = self.client.get(
            reverse("api:news:core:posts-list"), {"saved": "true"}
        )

        data = response.json()
        posts = data["results"]

        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(data["results"]), 10)

        for post in posts:
            with self.subTest(post=post):
                self.assertEqual(post["saved"], True)
