from fastapi.testclient import TestClient

from app.main import app

client = TestClient(app)


def test_predict_heuristic():
    resp = client.post(
        "/avm/predict",
        json={
            "area": 80.0,
            "district": "Cầu Giấy",
            "city": "Hà Nội",
            "property_type": "apartment",
            "bedrooms": 2,
            "bathrooms": 2,
            "floors": 1,
            "frontage": 0,
            "road_width": 0,
            "has_legal_paper": True,
        },
    )
    assert resp.status_code == 200
    data = resp.json()
    assert data["estimated_price_vnd"] > 0
    assert 0 <= data["confidence"] <= 1
    assert data["price_per_m2"] > 0
    assert data["price_range_low"] < data["estimated_price_vnd"]
    assert data["price_range_high"] > data["estimated_price_vnd"]


def test_predict_validation_error():
    resp = client.post(
        "/avm/predict",
        json={"area": -10, "district": "", "city": "HN", "property_type": "house"},
    )
    assert resp.status_code == 422


def test_extract_features():
    text = "Bán căn hộ chung cư 80m2 3 phòng ngủ 2 WC tầng 10 giá 3.5 tỷ sổ đỏ chính chủ"
    resp = client.post("/avm/extract-features", json={"text": text})
    assert resp.status_code == 200
    data = resp.json()
    features = data["features"]
    assert features["area"] == 80.0
    assert features["bedrooms"] == 3
    assert features["bathrooms"] == 2
    assert features["property_type"] == "apartment"
    assert features["has_legal_paper"] is True
    assert features["price_mentioned"] == 3_500_000_000


def test_extract_features_minimal():
    resp = client.post("/avm/extract-features", json={"text": "Bán nhà riêng"})
    assert resp.status_code == 200
    data = resp.json()
    assert data["features"]["property_type"] == "house"
