def test_template_audit_can_mark_and_restore_without_deleting(authenticated_client, app):
    from app_new.core.models.audit import TemplateAuditMark

    template_path = 'templates/admin/template_debug.html'
    response = authenticated_client.get('/admin/debug/templates?filter=all')
    assert response.status_code == 200
    assert 'Audit des templates' in response.get_data(as_text=True)
    assert 'Architecture cible des templates' in response.get_data(as_text=True)
    assert 'app_new/chatbot/templates/chatbot/index.html' in response.get_data(as_text=True)
    assert '/chatbot/' in response.get_data(as_text=True)
    assert 'Identiques' in response.get_data(as_text=True)
    assert 'Divergents' in response.get_data(as_text=True)

    response = authenticated_client.get('/admin/debug/templates/preview?path=templates%2Fadmin%2Ftemplate_debug.html&demo=1')
    assert response.status_code == 200
    assert 'Prévisualisation du template' in response.get_data(as_text=True)
    assert 'sandbox' in response.get_data(as_text=True)
    assert 'Données fictives actives' in response.get_data(as_text=True)

    chatbot_preview = authenticated_client.get('/admin/debug/templates/preview?path=chatbot%2Ftemplates%2Findex.html&demo=1')
    assert chatbot_preview.status_code == 200
    assert 'Rendu incomplet' not in chatbot_preview.get_data(as_text=True)

    response = authenticated_client.post('/admin/debug/templates/mark', data={
        'path': template_path,
        'action': 'obsolete',
        'notes': 'Marquage de test',
        'return_filter': 'obsolete',
    })
    assert response.status_code == 302
    with app.app_context():
        mark = TemplateAuditMark.query.filter_by(path=template_path).one()
        assert mark.notes == 'Marquage de test'

    response = authenticated_client.post('/admin/debug/templates/mark', data={
        'path': template_path,
        'action': 'restore',
        'return_filter': 'all',
    })
    assert response.status_code == 302
    with app.app_context():
        assert TemplateAuditMark.query.filter_by(path=template_path).first() is None
