"""§22.4b — a project with initial_state='active' lands new entries active +
unreviewed; the default 'super-draft' project is unchanged."""
from __future__ import annotations

from fastapi.testclient import TestClient

from test_propose_vertical import (  # noqa: F401
    app_with_fake_gitea, tmp_env, provision_user_row, sign_in_as,
)


def _propose(client):
    return client.post("/api/rfcs/propose", json={
        "title": "Active Lander", "slug": "active-lander",
        "pitch": "Lands active.", "tags": [],
    })


def test_super_draft_default_unchanged(app_with_fake_gitea):
    from app import entry as entry_mod
    app, fake = app_with_fake_gitea
    with TestClient(app) as client:
        provision_user_row(user_id=1, login="ben", role="owner")
        sign_in_as(client, user_id=1, gitea_login="ben", display_name="Ben", role="owner")
        assert _propose(client).status_code == 200
        f = fake.files[("wiggleverse", "meta", "propose/active-lander", "rfcs/active-lander.md")]
        e = entry_mod.parse(f["content"])
        assert e.state == "super-draft"
        assert e.unreviewed is False


def test_active_initial_state_lands_active_unreviewed(app_with_fake_gitea):
    from app import db, entry as entry_mod
    app, fake = app_with_fake_gitea
    with TestClient(app) as client:
        db.conn().execute("UPDATE collections SET initial_state='active' WHERE project_id='default'")
        provision_user_row(user_id=1, login="ben", role="owner")
        sign_in_as(client, user_id=1, gitea_login="ben", display_name="Ben", role="owner")
        assert _propose(client).status_code == 200
        f = fake.files[("wiggleverse", "meta", "propose/active-lander", "rfcs/active-lander.md")]
        e = entry_mod.parse(f["content"])
        assert e.state == "active"
        assert e.unreviewed is True
