# (c) cavaliba.com - test_datatask_views

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

from tests import helper
import app_home.cache as cache

from app_data.models import DataTask
from app_data.tasks import create_datatask, finish_task


class TaskNavbarPartialTest(TestCase):

    fixtures = ["test"]

    def setUp(self):
        cache.clear()

    @override_settings(CAVALIBA_AUTH_MODE="unittest")
    def test_returns_200(self):
        helper.add_user_with_perm(login="unittest", perms=["p_task_view"])
        response = self.client.get(reverse('app_data:task_navbar_partial'))
        self.assertEqual(response.status_code, 200)

    def test_no_auth_returns_empty(self):
        response = self.client.get(reverse('app_data:task_navbar_partial'))
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.content, b"")

    @override_settings(CAVALIBA_AUTH_MODE="unittest")
    def test_shows_running_task(self):
        helper.add_user_with_perm(login="unittest", perms=["p_task_view"])
        dt = create_datatask("my long task", owner_id="unittest")
        dt.state = "RUNNING"
        dt.save()
        response = self.client.get(reverse('app_data:task_navbar_partial'))
        self.assertEqual(response.status_code, 200)
        self.assertIn(b"my long task", response.content)

    @override_settings(CAVALIBA_AUTH_MODE="unittest")
    def test_no_tasks_returns_idle_state(self):
        helper.add_user_with_perm(login="unittest", perms=["p_task_view"])
        response = self.client.get(reverse('app_data:task_navbar_partial'))
        self.assertEqual(response.status_code, 200)
        # No active tasks — idle state rendered, no running indicator
        self.assertIn(b"datatask-label-idle", response.content)
        self.assertNotIn(b"datatask-label-running", response.content)


class TaskListViewTest(TestCase):

    fixtures = ["test"]

    def setUp(self):
        cache.clear()

    def test_requires_auth(self):
        response = self.client.get(reverse('app_data:task_list'))
        # Unauthenticated → redirect to login
        self.assertNotEqual(response.status_code, 200)

    @override_settings(CAVALIBA_AUTH_MODE="unittest")
    def test_empty_list(self):
        helper.add_user_with_perm(login="unittest", perms=["p_task_view"])
        response = self.client.get(reverse('app_data:task_list'))
        self.assertEqual(response.status_code, 200)

    @override_settings(CAVALIBA_AUTH_MODE="unittest")
    def test_shows_own_tasks(self):
        helper.add_user_with_perm(login="unittest", perms=["p_task_view"])
        create_datatask("task A", owner_id="unittest")
        response = self.client.get(reverse('app_data:task_list'))
        self.assertEqual(response.status_code, 200)
        self.assertIn(b"task A", response.content)


class TaskAdminListViewTest(TestCase):

    fixtures = ["test"]

    def setUp(self):
        cache.clear()

    @override_settings(CAVALIBA_AUTH_MODE="unittest")
    def test_admin_access(self):
        helper.add_admin_user(login="unittest")
        response = self.client.get(reverse('app_data:task_list'))
        self.assertEqual(response.status_code, 200)

    @override_settings(CAVALIBA_AUTH_MODE="unittest")
    def test_non_admin_redirected(self):
        helper.add_user_with_perm(login="unittest", perms=["p_data_access"])
        response = self.client.get(reverse('app_data:task_list'))
        self.assertNotEqual(response.status_code, 200)

    @override_settings(CAVALIBA_AUTH_MODE="unittest")
    def test_shows_all_tasks(self):
        helper.add_admin_user(login="unittest")
        create_datatask("task B", owner_id="someuser")
        response = self.client.get(reverse('app_data:task_list'))
        self.assertEqual(response.status_code, 200)
        self.assertIn(b"task B", response.content)

    @override_settings(CAVALIBA_AUTH_MODE="unittest")
    def test_state_filter(self):
        helper.add_admin_user(login="unittest")
        dt = create_datatask("done task", owner_id="u1")
        finish_task(dt.handle, state="DONE")
        create_datatask("queued task", owner_id="u1")
        response = self.client.get(reverse('app_data:task_list') + '?state=DONE')
        self.assertEqual(response.status_code, 200)
        self.assertIn(b"done task", response.content)
        self.assertNotIn(b"queued task", response.content)


class TaskAdminStopViewTest(TestCase):

    fixtures = ["test"]

    def setUp(self):
        cache.clear()

    @override_settings(CAVALIBA_AUTH_MODE="unittest")
    def test_stop_queued_task(self):
        helper.add_admin_user(login="unittest")
        dt = create_datatask("t", owner_id="u1")
        response = self.client.post(
            reverse('app_data:task_stop', kwargs={'handle': dt.handle})
        )
        self.assertEqual(response.status_code, 302)
        dt.refresh_from_db()
        self.assertEqual(dt.state, "ABORTED")

    @override_settings(CAVALIBA_AUTH_MODE="unittest")
    def test_get_does_not_stop(self):
        helper.add_admin_user(login="unittest")
        dt = create_datatask("t", owner_id="u1")
        self.client.get(
            reverse('app_data:task_stop', kwargs={'handle': dt.handle})
        )
        dt.refresh_from_db()
        self.assertEqual(dt.state, "QUEUED")
