from django.test import TestCase
from rest_framework.test import APIClient

from apps.billboard.models import Line, Post
from apps.lyric.models import User

class BaseAPITest(TestCase):
    # Helper fns
    def setUp(self):
        self.client = APIClient()
        self.user = User.objects.create_user("test@example.com")
        self.client.force_authenticate(user=self.user)

class PostDetailAPITest(BaseAPITest):
    def test_returns_post_with_lines(self):
        post = Post.objects.create(owner=self.user)
        Line.objects.create(text="line 1", post=post, author=self.user)
        Line.objects.create(text="line 2", post=post, author=self.user)

        response = self.client.get(f"/api/posts/{post.id}/")

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data["id"], str(post.id))
        self.assertEqual(len(response.data["lines"]), 2)

class PostLinesAPITest(BaseAPITest):
    def test_can_add_line_to_post(self):
        post = Post.objects.create(owner=self.user)

        response = self.client.post(
            f"/api/posts/{post.id}/lines/",
            {"text": "a new line"},
        )

        self.assertEqual(response.status_code, 201)
        self.assertEqual(Line.objects.count(), 1)
        self.assertEqual(Line.objects.first().text, "a new line")

    def test_cannot_add_empty_line_to_post(self):
        post = Post.objects.create(owner=self.user)

        response = self.client.post(
            f"/api/posts/{post.id}/lines/",
            {"text": ""},
        )

        self.assertEqual(response.status_code, 400)
        self.assertEqual(Line.objects.count(), 0)

    def test_cannot_add_duplicate_line_to_post(self):
        post = Post.objects.create(owner=self.user)
        Line.objects.create(text="post line", post=post, author=self.user)
        duplicate_response = self.client.post(
            f"/api/posts/{post.id}/lines/",
            {"text": "post line"},
        )

        self.assertEqual(duplicate_response.status_code, 400)
        self.assertEqual(Line.objects.count(), 1)

class PostsAPITest(BaseAPITest):
    def test_get_returns_only_users_posts(self):
        post1 = Post.objects.create(owner=self.user)
        Line.objects.create(text="line 1", post=post1, author=self.user)
        other_user = User.objects.create_user("other@example.com")
        Post.objects.create(owner=other_user)

        response = self.client.get("/api/posts/")

        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.data), 1)
        self.assertEqual(response.data[0]["id"], str(post1.id))

    def test_post_creates_post_with_line(self):
        response = self.client.post(
            "/api/posts/",
            {"text": "first line"},
        )

        self.assertEqual(response.status_code, 201)
        self.assertEqual(Post.objects.count(), 1)
        self.assertEqual(Post.objects.first().owner, self.user)
        self.assertEqual(Line.objects.first().text, "first line")

class UserSearchAPITest(BaseAPITest):
    def test_returns_users_matching_username(self):
        disco = User.objects.create_user("disco@example.com")
        disco.username = "discoman"
        disco.searchable = True
        disco.save()

        response = self.client.get("/api/users/?q=disc")

        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.data), 1)
        self.assertEqual(response.data[0]["username"], "discoman")

    def test_non_searchable_users_are_excluded(self):
        alice = User.objects.create_user("alice@example.com")
        alice.username = "princessAli"
        alice.save()  # searchable defaults to False

        response = self.client.get("/api/users/?q=prin")

        self.assertEqual(response.data, [])

    def test_response_does_not_include_email(self):
        alice = User.objects.create_user("alice@example.com")
        alice.username = "princessAli"
        alice.searchable = True
        alice.save()

        response = self.client.get("/api/users/?q=prin")

        self.assertNotIn("email", response.data[0])
