import uuid
from django.test import TestCase
from django.urls import reverse
from unittest.mock import patch
from backend.accounts.models import EmailConfirmation
from backend.accounts.utils import send_confirmation_email
from django.utils.timezone import now, timedelta


class EmailConfirmationTests(TestCase):
    def setUp(self):
        """Set up test data."""
        self.valid_email = "test@example.com"
        self.confirmation = EmailConfirmation.objects.create(
            email=self.valid_email,
            uuid=uuid.uuid4(),
            created_at=now()
        )

    @patch("backend.accounts.views.create_document_for_organization.delay")
    def test_confirm_email_valid(self, mock_task):
        """Test valid email confirmation."""
        response = self.client.get(reverse("confirm_email", args=[self.confirmation.uuid]))
        self.assertTemplateUsed(response, "accounts/confirmation_success.html")
        self.assertContains(response, self.valid_email)
        mock_task.assert_called_once_with(self.valid_email)

    @patch("backend.accounts.views.send_confirmation_email")
    def test_resend_confirmation(self, mock_send):
        """Test resending confirmation email."""
        response = self.client.post(reverse("resend_confirmation", args=[self.valid_email]))
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.content.decode(), "Confirmation email resent")
        mock_send.assert_called_once_with(self.valid_email)
