import pytest
from django.core import mail
from django.contrib.auth.models import User
from django.urls import reverse
from apps.carers.models import Profile
from apps.carers.views import CarerReviewView
from unittest.mock import Mock

from apps.orgs.models import Organisation


@pytest.mark.django_db
def test_send_review_notification_email_to_organisation():
    # Create test user and profile
    user = User.objects.create_user(username='johndoe', email='user@example.com', first_name='John', last_name='Doe')
    # Create an organisation and associate it with the user's profile
    organisation = Organisation.objects.create(name='Test Organisation', email='org@example.com')
    user.profile.organisations.add(organisation)

    # Mock the request object
    mock_request = Mock()
    mock_request.build_absolute_uri.return_value = 'http://testserver/org_login'

    # Create an instance of CarerReviewView
    view = CarerReviewView()
    view.request = mock_request

    # Call the method
    view.send_review_notification_email_to_organisation(user)

    # Check email was sent
    assert len(mail.outbox) == 1
    sent_email = mail.outbox[0]

    # Verify email content
    assert sent_email.subject == 'John Doe received a new Special Mention'
    assert organisation.name in sent_email.body
    assert user.first_name in sent_email.body
    assert user.last_name in sent_email.body
    assert 'http://testserver/org_login' in sent_email.body
    assert sent_email.to == ['user@example.com']
