from unittest import mock

from directory_api_client.client import api_client
from directory_constants import expertise
import pytest
import requests

from django.urls import reverse

from core.helpers import CompanyParser
from core.tests.helpers import create_response
from investment_support_directory import forms, views


@pytest.fixture(autouse=True)
def mock_retrieve_company(retrieve_profile_data):
    patch = mock.patch.object(
        api_client.company, 'retrieve_public_profile',
        return_value=create_response(200, retrieve_profile_data)
    )
    yield patch.start()
    patch.stop()


@pytest.fixture()
def mock_retrieve_company_non_isd(retrieve_profile_data):
    retrieve_profile_data['is_published_investment_support_directory'] = False
    patch = mock.patch.object(
        api_client.company, 'retrieve_public_profile',
        return_value=create_response(200, retrieve_profile_data)
    )
    yield patch.start()
    patch.stop()


def test_profile(client, retrieve_profile_data):
    company = CompanyParser(retrieve_profile_data)

    url = reverse(
        'investment-support-directory:profile',
        kwargs={
            'company_number': retrieve_profile_data['number'],
            'slug': retrieve_profile_data['slug'],
        }
    )

    response = client.get(url)

    assert response.status_code == 200
    assert response.context_data['company'] == company.serialize_for_template()


def test_profile_querystring(client, retrieve_profile_data):
    company = CompanyParser(retrieve_profile_data)

    url = reverse(
        'investment-support-directory:profile',
        kwargs={
            'company_number': retrieve_profile_data['number'],
            'slug': retrieve_profile_data['slug'],
        }
    )

    response = client.get(url, {'q': '123'})

    assert response.status_code == 200
    assert response.context_data['company'] == company.serialize_for_template()
    assert response.context_data['search_querystring'] == 'q=123'


def test_profile_slug_redirect(client, retrieve_profile_data):
    url = reverse(
        'investment-support-directory:profile',
        kwargs={
            'company_number': retrieve_profile_data['number'],
            'slug': 'something',
        }
    )

    response = client.get(url)

    assert response.status_code == 302
    assert response.url == reverse(
        'investment-support-directory:profile',
        kwargs={
            'company_number': retrieve_profile_data['number'],
            'slug': retrieve_profile_data['slug'],
        }
    )


def test_trade_redirect(client):

    url = '/trade' + reverse(
        'investment-support-directory:search',
    )
    response = client.get(url)

    assert response.status_code == 302
    assert response.url == '/investment-support-directory/'


def test_profile_calls_api(
    mock_retrieve_company, client, retrieve_profile_data
):
    url = reverse(
        'investment-support-directory:profile',
        kwargs={
            'company_number': retrieve_profile_data['number'],
            'slug': retrieve_profile_data['slug'],
        }
    )
    response = client.get(url)

    assert response.status_code == 200
    assert mock_retrieve_company.call_count == 1
    assert mock_retrieve_company.call_args == mock.call(
        number=retrieve_profile_data['number']
    )


def test_get_profile_404_non_investment_support_directory(
    mock_retrieve_company_non_isd, client, retrieve_profile_data
):
    url = reverse(
        'investment-support-directory:profile',
        kwargs={
            'company_number': retrieve_profile_data['number'],
            'slug': retrieve_profile_data['slug'],
        }
    )
    response = client.get(url)