"""Couverture de la navigation métier introduite en phase 1.6."""

from app_new import db
from app_new.core.models.rbac import Role, UserRole
from app_new.core.models.user import User


def test_admin_sees_new_menu_and_legacy_reference(authenticated_client):
    response = authenticated_client.get("/auth/profile")
    assert response.status_code == 200
    body = response.get_data(as_text=True)
    for label in ("Mon travail", "Patrimoine", "Produits &amp; stocks", "Documents", "Administration"):
        assert label in body
    assert "Ancien menu" in body
    assert "Référentiel temporaire" in body or "temporaire" in body
    assert body.index("Administration") < body.index("Ancien menu")
    for group in ("Principal", "Interventions", "Équipements", "Documents", "Configuration"):
        assert group in body
    for historical_label in (
        "Ajouter un compte Outlook",
        "Dossiers Outlook surveillés",
        "Messages ENT",
        "Interprétations ENT",
        "Personnels ENT",
        "PRONOTE (sync salles)",
    ):
        assert historical_label in body


def test_historical_integration_endpoints_are_registered(app):
    """Les liens conditionnels historiques pointent vers des routes existantes.

    Le formulaire Pronote n'est pas soumis : ce contrôle ne déclenche aucune
    synchronisation externe.
    """
    with app.app_context():
        endpoints = {rule.endpoint for rule in app.url_map.iter_rules()}
    for endpoint in (
        "outlook_pages.add_account",
        "outlook_dashboard.processed_folders_config",
        "ent.messages",
        "ent.interpretations",
        "ent.staff",
        "pronote.sync_salles",
    ):
        assert endpoint in endpoints


def test_non_admin_does_not_see_admin_navigation(client, app):
    with app.app_context():
        role = Role.query.filter_by(slug="lecture", is_active=True).first()
        user = User(username="phase16_lecture", email="phase16_lecture@gmao.local", full_name="Lecture phase 1.6", is_active=True)
        user.set_password("phase16-lecture-password")
        db.session.add(user)
        db.session.flush()
        db.session.add(UserRole(user_id=user.id, role_id=role.id))
        db.session.commit()
        user_id = user.id
    try:
        login = client.post("/auth/login", data={"username": "phase16_lecture", "password": "phase16-lecture-password"})
        assert login.status_code == 302
        response = client.get("/auth/profile")
        assert response.status_code == 200
        body = response.get_data(as_text=True)
        assert "Administration" not in body
        assert "Ancien menu" not in body
    finally:
        with app.app_context():
            user = db.session.get(User, user_id)
            if user:
                db.session.delete(user)
                db.session.commit()


def test_new_menu_destinations_do_not_return_accidental_404_or_500(authenticated_client):
    paths = (
        "/interventions/",
        "/planning/rooms",
        "/planning/my-day",
        "/planning/",
        "/equipments/",
        "/equipments/buildings/",
        "/equipments/zones/",
        "/equipments/rooms/",
        "/cleaning/products",
        "/cleaning/references",
        "/cleaning/stock",
        "/companies/",
        "/documents/",
        "/documents/fds",
        "/admin/users",
        "/admin/roles",
        "/admin/permissions",
    )
    for path in paths:
        response = authenticated_client.get(path)
        assert response.status_code not in (404, 500), path


def test_my_day_replanning_rejects_invalid_slot_without_write(authenticated_client):
    response = authenticated_client.post(
        "/planning/my-day/reschedule",
        data={"source_type": "scheduled_task", "source_id": "999999", "date": "2026-08-24", "slot": "bad"},
    )
    assert response.status_code == 302
