# Copyright (C) 2018-2025 by BDE ENS Paris-Saclay
# SPDX-License-Identifier: GPL-3.0-or-later

from datetime import timedelta

from api.tests import TestAPI
from django.contrib.auth.models import User
from django.test import TestCase
from django.urls import reverse
from django.utils import timezone

from ..api.views import WrappedViewSet, BdeViewSet
from ..models import Bde, Wrapped


class TestWrapped(TestCase):
    """
    Test activities
    """
    fixtures = ('initial',)

    def setUp(self):
        self.user = User.objects.create_superuser(
            username="admintoto",
            password="tototototo",
            email="toto@example.com"
        )
        self.client.force_login(self.user)

        sess = self.client.session
        sess["permission_mask"] = 42
        sess.save()

        self.bde = Bde.objects.create(
            name="The best BDE",
            date_start=timezone.now() - timedelta(days=365),
            date_end=timezone.now(),
        )

        self.wrapped = Wrapped.objects.create(
            generated=True,
            public=False,
            bde=self.bde,
            note=self.user.note,
            data_json="{}",
        )

    def test_wrapped_list(self):
        """
        Display the list of all wrapped
        """
        response = self.client.get(reverse("wrapped:wrapped_list"))
        self.assertEqual(response.status_code, 200)

    def test_wrapped_detail(self):
        """
        Display the detail of an wrapped
        """
        response = self.client.get(reverse("wrapped:wrapped_detail", args=(self.wrapped.pk,)))
        self.assertEqual(response.status_code, 200)


class TestWrappedAPI(TestAPI):
    def setUp(self) -> None:
        super().setUp()

        self.bde = Bde.objects.create(
            name="The best BDE",
            date_start=timezone.now() - timedelta(days=365),
            date_end=timezone.now(),
        )

        self.wrapped = Wrapped.objects.create(
            generated=True,
            public=False,
            bde=self.bde,
            note=self.user.note,
            data_json="{}",
        )

    def test_bde_api(self):
        """
        Load Bde API page and test all filters and permissions
        """
        self.check_viewset(BdeViewSet, "/api/wrapped/bde/")

    def test_wrapped_api(self):
        """
        Load Wrapped API page and test all filters and permissions
        """
        self.check_viewset(WrappedViewSet, "/api/wrapped/wrapped/")
