import json
import uuid
from unittest.mock import patch

import django_rq
from django.http import HttpResponse
from django.urls import reverse
from requests import Session
from rest_framework import status

from extras.models import Webhook
from extras.webhooks import enqueue_object, flush_webhooks, generate_signature
from extras.workers import generate_signature, process_webhook
from peering.models import AutonomousSystem
from utils.enums import ObjectChangeAction
from utils.models import Tag
from utils.testing import APITestCase


class WebhookTest(APITestCase):
    def setUp(self):
        super().setUp()

        # Make sure the queue is empty before testing
        self.queue = django_rq.get_queue("default")
        self.queue.empty()

    @classmethod
    def setUpTestData(cls):
        TEST_URL = "http://localhost/"
        TEST_SECRET = "thisisaverystrongsecret"

        webhooks = Webhook.objects.bulk_create(
            [
                Webhook(
                    name="Create Webhook",
                    type_create=True,
                    url=TEST_URL,
                    secret=TEST_SECRET,
                ),
                Webhook(
                    name="Update Webhook",
                    type_update=True,
                    url=TEST_URL,
                    secret=TEST_SECRET,
                ),
                Webhook(
                    name="Delete Webhook",
                    type_delete=True,
                    url=TEST_URL,
                    secret=TEST_SECRET,
                ),
            ]
        )
        Tag.objects.bulk_create(
            (
                Tag(name="Foo", slug="foo"),
                Tag(name="Bar", slug="bar"),
                Tag(name="Baz", slug="baz"),
            )
        )

    def test_enqueue_webhook_create(self):
        data = {
            "asn": 64500,
            "name": "AS 1",
            "tags": [
                {"name": "Foo"},
                {"name": "Bar"},
            ],
        }
        url = reverse("peering-api:autonomoussystem-list")
        response = self.client.post(url, data, format="json", **self.header)