"""
Author: Michel Peltriaux
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
Contact: michel.peltriaux@sgdnord.rlp.de
Created on: 28.01.22

"""

import json

from django.core.exceptions import ObjectDoesNotExist
from django.urls import reverse

from api.tests.v1.share.test_api_sharing import BaseAPIV1TestCase


class APIV1DeleteTestCase(BaseAPIV1TestCase):
    @classmethod
    def setUpTestData(cls):
        super().setUpTestData()

    def _run_delete_request(self, url):
        response = self.client.delete(
            url,
            **self.header_data
        )
        return response

    def _test_delete_object(self, obj, url):
        """ Tests the API DELETE of a data object.

        Args:
            url (str): The api delete url

        Returns:

        """
        obj.refresh_from_db()
        _id = obj.id
        self.assertIsNotNone(_id)
        self.assertIsNone(obj.deleted)

        response = self._run_delete_request(url)
        content = json.loads(response.content)

        self.assertEqual(response.status_code, 200, msg=response.content)
        self.assertTrue(content.get("success", False), msg=response.content)

        obj.refresh_from_db()
        self.assertIsNotNone(obj.deleted)
        self.assertEqual(obj.deleted.user, self.superuser)

    def test_delete_intervention(self):
        """ Tests api creation of bare minimum interventions

        Returns:

        """
        test_intervention = self.create_dummy_intervention()
        test_intervention.share_with_user(self.superuser)
        url = reverse("api:v1:intervention", args=(str(test_intervention.id),))
        self._test_delete_object(test_intervention, url)

    def test_delete_compensation(self):
        """ Tests api creation of bare minimum interventions

        Returns:

        """
        test_comp = self.create_dummy_compensation()
        test_comp.share_with_user(self.superuser)
        url = reverse("api:v1:compensation", args=(str(test_comp.id),))
        self._test_delete_object(test_comp, url)

    def test_delete_eco_account(self):
        """ Tests api creation of bare minimum interventions

        Returns:

        """
        test_acc = self.create_dummy_eco_account()
        test_acc.share_with_user(self.superuser)
        url = reverse("api:v1:ecoaccount", args=(str(test_acc.id),))
        self._test_delete_object(test_acc, url)

    def test_delete_ema(self):
        """ Tests api creation of bare minimum interventions

        Returns:

        """
        test_ema = self.create_dummy_ema()
        test_ema.share_with_user(self.superuser)
        url = reverse("api:v1:ema", args=(str(test_ema.id),))
        self._test_delete_object(test_ema, url)

    def test_delete_deduction(self):
        """ Tests api creation of bare minimum interventions

        Returns:

        """
        test_deduction = self.create_dummy_deduction()
        test_deduction.intervention.share_with_user(self.superuser)
        url = reverse("api:v1:deduction", args=(str(test_deduction.id),))

        response = self._run_delete_request(url)
        content = json.loads(response.content)

        self.assertEqual(response.status_code, 200, msg=response.content)
        self.assertTrue(content.get("success", False), msg=response.content)

        try:
            test_deduction.refresh_from_db()
            self.fail("Deduction is not deleted from db!")
        except ObjectDoesNotExist:
            pass

