from django.conf import settings
from django.utils import timezone
import pytest
from django.test import Client
from django.contrib.auth import get_user_model

from generic.tests.conftest import create_modality, create_examination, create_plane, create_contrast, create_user

from atlas.models import (
    Case,
    Finding,
    Condition,
    ConditionRelationship,
    Presentation,
    Subspecialty,
    PathologicalProcess,
    Differential,
    Structure,
    Series,
    SeriesImage,
    SeriesFinding,
    CaseCollection,
    SeriesDetail,
    CaseDetail,
    BaseReportAnswer,
    CidReportAnswer,
    UserReportAnswer,
    SelfReview,
    UncategorisedDicom,
    DuplicateDicom,
)


@pytest.fixture
def create_finding():
    return Finding.objects.create(name="Test Finding")


@pytest.fixture
def create_condition():
    return Condition.objects.create(name="Test Condition")


@pytest.fixture
def create_condition_relationship(create_condition):
    return ConditionRelationship.objects.create(
        child=create_condition, parent=create_condition
    )


@pytest.fixture
def create_presentation():
    return Presentation.objects.create(name="Test Presentation")


@pytest.fixture
def create_subspecialty():
    return Subspecialty.objects.create(name="Test Subspecialty")


@pytest.fixture
def create_pathological_process():
    return PathologicalProcess.objects.create(name="Test Pathological Process")


@pytest.fixture
def create_differential(create_condition, create_case):
    return Differential.objects.create(condition=create_condition, case=create_case)


@pytest.fixture
def create_structure():
    return Structure.objects.create(name="Test Structure")


@pytest.fixture
def create_case(
    create_subspecialty,
    create_condition,
    create_presentation,
    create_pathological_process,
):
    subspecialty = create_subspecialty
    condition = create_condition
    presentation = create_presentation
    pathological_process = create_pathological_process
    user = get_user_model().objects.create(
        username="Test User", password="Test Password", email="testuser@example.com"
    )

    case = Case.objects.create(
        title="Sample Case",
        description="Sample description",
        history="Sample history",
        discussion="Sample discussion",
        report="Sample report",
        diagnostic_certainty=Case.CertaintyChoices.LIKELY,
        verified=True,
        created_date=timezone.now(),
        published_date=timezone.now(),
        archive=False,
        open_access=True,
    )

    case.subspecialty.add(subspecialty)
    case.condition.add(condition)
    case.presentation.add(presentation)
    case.pathological_process.add(pathological_process)
    case.author.add(user)

    return case

@pytest.fixture
def make_case(create_subspecialty, create_condition, create_presentation, create_pathological_process):
    def _make_case(
        title="Sample Case",
        description="Sample description",
        history="Sample history",
        discussion="Sample discussion",
        report="Sample report",
        diagnostic_certainty=Case.CertaintyChoices.LIKELY,
        verified=True,
        created_date=timezone.now(),
        published_date=timezone.now(),
        archive=False,
        open_access=True,
    ):
        subspecialty = create_subspecialty
        condition = create_condition
        presentation = create_presentation
        pathological_process = create_pathological_process
        user, _ = get_user_model().objects.get_or_create(
            username="Test User 2", password="Test Password", email="test@emai.com")

        case = Case.objects.create(
            title=title,
            description=description,
            history=history,
            discussion=discussion,
            report=report,
            diagnostic_certainty=diagnostic_certainty,
            verified=verified,
            created_date=created_date,
            published_date=published_date,
            archive=archive,
            open_access=open_access,
        )
        case.subspecialty.add(subspecialty)
        case.condition.add(condition)
        case.presentation.add(presentation)
        case.pathological_process.add(pathological_process)
        case.author.add(user)

        return case
    return _make_case

@pytest.fixture
def create_series(create_modality, create_examination, create_plane, create_contrast, create_user):
    author=create_user
    series_instance_uid="1234567890"
    modality = create_modality
    examination = create_examination
    plane = create_plane
    contrast = create_contrast
    
    series = Series.objects.create(
        modality=modality,
        examination=examination,
        plane=plane,
        contrast=contrast,
        series_instance_uid=series_instance_uid
    )

    series.author.add(author)
    
    return series
    

@pytest.fixture
def create_series_image():
    return SeriesImage.objects.create()


@pytest.fixture
def create_series_finding():
    return SeriesFinding.objects.create(description="Test Description")


@pytest.fixture
def create_case_collection():
    return CaseCollection.objects.create(name="Test Collection")


@pytest.fixture
def create_series_detail():
    return SeriesDetail.objects.create()


@pytest.fixture
def create_case_detail():
    return CaseDetail.objects.create()


@pytest.fixture
def create_base_report_answer():
    return BaseReportAnswer.objects.create()


@pytest.fixture
def create_cid_report_answer():
    return CidReportAnswer.objects.create()


@pytest.fixture
def create_user_report_answer():
    return UserReportAnswer.objects.create()


@pytest.fixture
def create_self_review():
    return SelfReview.objects.create()


@pytest.fixture
def create_uncategorised_dicom():
    return UncategorisedDicom.objects.create()


@pytest.fixture
def create_duplicate_dicom():
    return DuplicateDicom()
