import os
import duckdb
import pytest


@pytest.mark.asyncio
async def test_learn_resync_endpoint(async_client):
    # Ensure require_admin is permissive in dev
    os.environ['ENV'] = 'development'
    # Seed learning duckdb
    db_path = 'data/learning.duckdb'
    os.makedirs('data', exist_ok=True)
    with duckdb.connect(db_path) as conn:
        conn.execute("""
            INSERT OR REPLACE INTO decision_log (job_id, tenant_id, segment, mode, threshold_policy, config_hash, sample_size, runtime_ms)
            VALUES ('jobX','00000000-0000-0000-0000-000000000001','default','match','high_recall','h',100,10)
        """)
        conn.execute("""
            INSERT OR IGNORE INTO feedback_events (job_id, match_id, outcome, user_id)
            VALUES ('jobX','m1','accepted','tester')
        """)

    res = await async_client.post('/api/v2/intelligence/learn/resync', json={"segment": "default", "lookback_days": 7})
    assert res.status_code == 200, res.text
    body = res.json()
    assert body['segment'] == 'default'
    assert 'policy_after' in body
