import pytest
from django.urls import reverse
from django.contrib.auth.models import User
from django.test import Client

SAFE_STATUS_CODES = {200, 301, 302, 303, 400, 403, 404, 405}

@pytest.fixture
def candidate_client(db):
    user = User.objects.create_user(username="candidate", password="password", email="candidate@test.com")
    client = Client()
    client.force_login(user)
    return client

@pytest.fixture
def superuser_client(db):
    user = User.objects.create_superuser(username="superuser", password="password", email="superuser@test.com")
    client = Client()
    client.force_login(user)
    return client

@pytest.fixture
def anonymous_client(db):
    return Client()

@pytest.fixture
def smoke_data(db):
    from oef.models import Entry, Formats
    entry = Entry.objects.create(
        modality="CT",
        exam_code="CT1",
        exam_name="CT Entry",
        questions="Question Text\nQuestion 2",
        questions_original="Question Text\nQuestion 2",
    )
    fmt = Formats.objects.create(
        name="Format 1",
        format="Question Text",
    )
    return {
        "entry": entry,
        "format": fmt,
    }

URL_SPECS = [
    ("oef:entry_table_view", None),
    ("oef:formats_view", None),
    ("oef:formats_dupe_view", None),
    ("oef:format_create", None),
    ("oef:format_search", None),
    ("oef:replace_questions", None),
    ("oef:questions", None),
    ("oef:subspecialties", None),
    ("oef:entries_table", None),
    ("oef:entries", None),
    ("oef:questions_diff", None),
    ("oef:questions_sym", None),
    ("oef:entries_by_question", None),
    ("oef:entries_by_question_codes", None),
    ("oef:entry_update", lambda d: {"pk": d["entry"].pk}),
    ("oef:entry_detail", lambda d: {"pk": d["entry"].pk}),
    ("oef:entry_detail_formats", lambda d: {"pk": d["entry"].pk}),
    ("oef:format_update", lambda d: {"pk": d["format"].pk}),
    ("oef:formats_apply", lambda d: {"pk": d["format"].pk}),
    ("oef:formats_delete", lambda d: {"pk": d["format"].pk}),
    ("oef:formats_save", None),
    ("oef:output", None),
]

@pytest.mark.django_db
@pytest.mark.parametrize("url_name, kwargs_lambda", URL_SPECS)
def test_url_smoke_anonymous(anonymous_client, smoke_data, url_name, kwargs_lambda):
    kwargs = kwargs_lambda(smoke_data) if kwargs_lambda else None
    url = reverse(url_name, kwargs=kwargs)
    response = anonymous_client.get(url, follow=False)
    assert response.status_code != 500, f"500 Server Error on {url_name} ({url}) for Anonymous"
    assert response.status_code in {200, 302, 400, 403, 404, 405}, f"Unexpected status code {response.status_code} on {url_name} for Anonymous"

@pytest.mark.django_db
@pytest.mark.parametrize("url_name, kwargs_lambda", URL_SPECS)
def test_url_smoke_candidate(candidate_client, smoke_data, url_name, kwargs_lambda):
    kwargs = kwargs_lambda(smoke_data) if kwargs_lambda else None
    url = reverse(url_name, kwargs=kwargs)
    response = candidate_client.get(url, follow=False)
    assert response.status_code != 500, f"500 Server Error on {url_name} ({url}) for Candidate"
    assert response.status_code in SAFE_STATUS_CODES, f"Unexpected status code {response.status_code} on {url_name} for Candidate"

@pytest.mark.django_db
@pytest.mark.parametrize("url_name, kwargs_lambda", URL_SPECS)
def test_url_smoke_superuser(superuser_client, smoke_data, url_name, kwargs_lambda):
    kwargs = kwargs_lambda(smoke_data) if kwargs_lambda else None
    url = reverse(url_name, kwargs=kwargs)
    response = superuser_client.get(url, follow=False)
    assert response.status_code != 500, f"500 Server Error on {url_name} ({url}) for Superuser"
    assert response.status_code in SAFE_STATUS_CODES, f"Unexpected status code {response.status_code} on {url_name} for Superuser"
