import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).parent.parent / "python-backend"))

from datetime import date, timedelta
from core.doc_state_machine import compute_doc_state, DocState

TODAY = date.today()


def test_medicare_ffs_pa_not_required():
    state = compute_doc_state(
        payer_type="medicare",
        csv_swo_status="On File",
        csv_visit_date=TODAY - timedelta(days=30),
        confirmed_visit_date=None,
        csv_pecos_verified="Yes",
        csv_pa_status=None,
        csv_diagnosis_on_file="Yes",
        is_transfer=False,
    )
    assert state.pa == "Not Required"
    assert state.pecos == "Verified"


def test_medicaid_pecos_not_applicable():
    state = compute_doc_state(
        payer_type="medicaid",
        csv_swo_status="On File",
        csv_visit_date=TODAY - timedelta(days=30),
        confirmed_visit_date=None,
        csv_pecos_verified=None,
        csv_pa_status="Approved",
        csv_diagnosis_on_file="Yes",
        is_transfer=False,
    )
    assert state.pecos == "N/A"
    assert state.pa == "Approved"


def test_transfer_patient_all_pending():
    state = compute_doc_state(
        payer_type="medicare",
        csv_swo_status=None,
        csv_visit_date=None,
        confirmed_visit_date=None,
        csv_pecos_verified=None,
        csv_pa_status=None,
        csv_diagnosis_on_file=None,
        is_transfer=True,
    )
    assert state.swo == "Pending — Verify"
    assert state.visit == "Pending — Verify"
    assert state.pecos == "Pending — Verify"
    assert state.pa == "Pending — Verify"
    assert state.diagnosis == "Pending — Verify"


def test_cascade_visit_not_confirmed():
    state = compute_doc_state(
        payer_type="medicare",
        csv_swo_status="On File",
        csv_visit_date=None,
        confirmed_visit_date=None,
        csv_pecos_verified="Yes",
        csv_pa_status=None,
        csv_diagnosis_on_file="Yes",
        is_transfer=False,
    )
    assert len(state.cascade) == 0


def test_no_cascade_when_all_clear():
    state = compute_doc_state(
        payer_type="medicare",
        csv_swo_status="On File",
        csv_visit_date=TODAY - timedelta(days=30),
        confirmed_visit_date=None,
        csv_pecos_verified="Yes",
        csv_pa_status=None,
        csv_diagnosis_on_file="Yes",
        is_transfer=False,
    )
    assert state.cascade == []
