from tests.storage.conftest import login_as_crm_user


def test_offer_description_endpoint_reads_runtime_description(client, tmp_path, monkeypatch):
    monkeypatch.setenv("DATA_DIR", str(tmp_path))
    offer_dir = tmp_path / "offers" / "77"
    offer_dir.mkdir(parents=True)
    (offer_dir / "description_77.md").write_text("  Opis z InvestmentData.  \n", encoding="utf-8")

    login_as_crm_user(client, master=True)
    resp = client.get("/crm/offers/description/77")

    assert resp.status_code == 200
    assert resp.mimetype == "text/plain"
    assert resp.get_data(as_text=True) == "Opis z InvestmentData."


def test_offer_description_endpoint_returns_empty_text_when_missing(client, tmp_path, monkeypatch):
    monkeypatch.setenv("DATA_DIR", str(tmp_path))

    login_as_crm_user(client, master=True)
    resp = client.get("/crm/offers/description/78")

    assert resp.status_code == 200
    assert resp.get_data(as_text=True) == ""


def test_offer_description_endpoint_requires_crm_session(client, tmp_path, monkeypatch):
    monkeypatch.setenv("DATA_DIR", str(tmp_path))
    offer_dir = tmp_path / "offers" / "79"
    offer_dir.mkdir(parents=True)
    (offer_dir / "description_79.md").write_text("Poufny opis", encoding="utf-8")

    resp = client.get("/crm/offers/description/79")

    assert resp.status_code == 403
    assert resp.get_data(as_text=True) != "Poufny opis"
