import pytest

pytestmark = pytest.mark.requires_postgres


def _admin() -> dict:
    return {"Authorization": "Bearer test-admin-token"}


def _seed_family(db_session) -> str:
    from uuid import uuid4
    from app.models import FamilyProfile

    fid = uuid4()
    db_session.add(
        FamilyProfile(
            id=fid,
            name="Test Family NS",
            household_size=4,
            adult_count=2,
            child_count=2,
            calorie_target=2400,
        )
    )
    db_session.commit()
    return str(fid)


def test_block_ingredient(client, db_session):
    fid = _seed_family(db_session)
    ing = client.post(
        "/api/admin/ingredients",
        json={"name": "Test NS Mushrooms", "aliases": [], "aisle": "produce", "unit": "oz"},
        headers=_admin(),
    )
    iid = ing.json()["id"]

    r = client.post(
        "/api/admin/never-suggest",
        json={"family_profile_id": fid, "ingredient_id": iid, "reason": "dislike"},
        headers=_admin(),
    )
    assert r.status_code == 201, r.text


def test_list_never_suggest_for_family(client, db_session):
    fid = _seed_family(db_session)
    ing = client.post(
        "/api/admin/ingredients",
        json={"name": "Test NS Cilantro", "aliases": [], "aisle": "produce", "unit": "tbsp"},
        headers=_admin(),
    )
    client.post(
        "/api/admin/never-suggest",
        json={"family_profile_id": fid, "ingredient_id": ing.json()["id"], "reason": "dislike"},
        headers=_admin(),
    )
    r = client.get(f"/api/never-suggest?family_profile_id={fid}")
    assert r.status_code == 200
    assert len(r.json()) >= 1


def test_unblock_removes_row(client, db_session):
    fid = _seed_family(db_session)
    ing = client.post(
        "/api/admin/ingredients",
        json={"name": "Test NS Anchovy", "aliases": [], "aisle": "pantry", "unit": "ea"},
        headers=_admin(),
    )
    create = client.post(
        "/api/admin/never-suggest",
        json={"family_profile_id": fid, "ingredient_id": ing.json()["id"], "reason": "dislike"},
        headers=_admin(),
    )
    nid = create.json()["id"]
    r = client.delete(f"/api/admin/never-suggest/{nid}", headers=_admin())
    assert r.status_code == 204
