from unittest.mock import PropertyMock

import pytest

from app.models.service import Service
from app.utils.branding import get_available_choices
from tests import organisation_json


@pytest.mark.parametrize('branding_type', ['email', 'letter'])
@pytest.mark.parametrize('org_type, expected_options', [
    ('central', []),
    ('local', []),
    ('nhs_central', [('nhs', 'NHS')]),
    ('nhs_local', [('nhs', 'NHS')]),
    ('nhs_gp', [('nhs', 'NHS')]),
    ('emergency_service', []),
    ('other', []),
])
def test_get_available_choices_service_not_assigned_to_org(
    service_one,
    branding_type,
    org_type,
    expected_options,
):
    service_one['organisation_type'] = org_type
    service = Service(service_one)

    options = get_available_choices(service, branding_type=branding_type)
    assert list(options) == expected_options


@pytest.mark.parametrize('branding_type', ['email', 'letter'])
@pytest.mark.parametrize('org_type, expected_options', [
    ('local', [('organisation', 'Test Organisation')]),
    ('nhs_central', [('nhs', 'NHS')]),
    ('nhs_local', [('nhs', 'NHS')]),
    ('nhs_gp', [('nhs', 'NHS')]),
    ('emergency_service', [('organisation', 'Test Organisation')]),
    ('other', [('organisation', 'Test Organisation')]),
])
def test_get_available_choices_service_assigned_to_org(
    mocker,
    service_one,
    branding_type,
    org_type,
    expected_options,
    mock_get_service_organisation,
):
    service = Service(service_one)

    mocker.patch(
        'app.organisations_client.get_organisation',
        return_value=organisation_json(organisation_type=org_type)
    )

    options = get_available_choices(service, branding_type=branding_type)
    assert list(options) == expected_options


@pytest.mark.parametrize('branding_type, expected_options', [
    ('email', [
        ('govuk_and_org', 'GOV.UK and Test Organisation'),
        ('organisation', 'Test Organisation'),
    ]),
    ('letter', [
        ('organisation', 'Test Organisation'),
    ])
])
def test_get_available_choices_with_central_org(
    mocker,
    service_one,
    branding_type,
    expected_options,
    mock_get_service_organisation,
):
    service = Service(service_one)

    mocker.patch(
        'app.organisations_client.get_organisation',
        return_value=organisation_json(organisation_type='central'),
    )

    options = get_available_choices(service, branding_type=branding_type)
    assert list(options) == expected_options


def test_get_available_choices_email_branding_set(
    mocker,
    service_one,
    mock_get_service_organisation,
    mock_get_email_branding,
):
    service = Service(service_one)

    mocker.patch(
        'app.organisations_client.get_organisation',
        return_value=organisation_json()
    )
    mocker.patch(
        'app.models.service.Service.email_branding_id',
        new_callable=PropertyMock,
        return_value='1234-abcd',
    )

    options = get_available_choices(service, branding_type='email')
    assert list(options) == [
        ('govuk', 'GOV.UK'),
        ('govuk_and_org', 'GOV.UK and Test Organisation'),
        ('organisation', 'Test Organisation'),
    ]


def test_get_available_choices_letter_branding_set(
    mocker,
    service_one,
    mock_get_service_organisation,
    mock_get_letter_branding_by_id,
):
    service = Service(service_one)

    mocker.patch(
        'app.organisations_client.get_organisation',
        return_value=organisation_json()
    )
    mocker.patch(
        'app.models.service.Service.letter_branding_id',
        new_callable=PropertyMock,
        return_value='1234-abcd',
    )

    options = get_available_choices(service, branding_type='letter')
    assert list(options) == [
        ('organisation', 'Test Organisation'),
    ]

@@ -1756,32 +1756,40 @@
 arametrize('
+service_
 branding
 _type, expec
@@ -1776,21 +1776,16 @@
 branding
-_type
 , expect
@@ -1804,23 +1804,20 @@
  %5B%0A    (
-'email'
+None
 , %5B%0A    
@@ -1936,18 +1936,109 @@
   ('
-letter', %5B
+1234-abcd', %5B%0A        ('govuk', 'GOV.UK'),%0A        ('govuk_and_org', 'GOV.UK and Test Organisation'),
 %0A   
@@ -2126,12 +2126,22 @@
 ces_
-with
+email_branding
 _cen
@@ -2179,32 +2179,40 @@
 ce_one,%0A    
+service_
 branding
 _type,%0A    e
@@ -2195,29 +2195,24 @@
 ice_branding
-_type
 ,%0A    expect
@@ -2262,430 +2262,8 @@
 on,%0A
-):%0A    service = Service(service_one)%0A%0A    mocker.patch(%0A        'app.organisations_client.get_organisation',%0A        return_value=organisation_json(organisation_type='central'),%0A    )%0A%0A    options = get_available_choices(service, branding_type=branding_type)%0A    assert list(options) == expected_options%0A%0A%0Adef test_get_available_choices_email_branding_set(%0A    mocker,%0A    service_one,%0A    mock_get_service_organisation,%0A
     
@@ -2428,33 +2428,61 @@
 ganisation_json(
-)
+organisation_type='central'),
 %0A    )%0A    mocke
@@ -2598,35 +2598,40 @@
 eturn_value=
-'1234-abcd'
+service_branding
 ,%0A    )%0A%0A   
@@ -2727,150 +2727,24 @@
  == 
-%5B%0A        ('govuk', 'GOV.UK'),%0A        ('govuk_and_org', 'GOV.UK and Test Organisation'),%0A        ('organisation', 'Test Organisation'),%0A    %5D
+expected_options
 %0A%0A%0Ad

Combine tests for central org email branding