"""逐次单券事件不能被同设备同日的每日去重记录串场。"""

from sqlalchemy import delete, select

from app.admin.repositories.coupon_data import _point_scores_by_trace, coupon_point_details
from app.db.session import SessionLocal
from app.models.coupon_state import CouponClaimEvent, CouponClaimRecord
from app.repositories.coupon_state import record_claims


def test_same_device_same_day_keeps_scores_for_each_trace() -> None:
    db = SessionLocal()
    device = "event-repeat-device"
    first_trace = "event-repeat-first"
    second_trace = "event-repeat-second"
    first_results = [
        {"coupon_id": "mt-repeat", "name": "美团测试券", "status": "success"},
        {"coupon_id": "tb-repeat", "name": "淘宝测试券", "status": "failed"},
    ]
    second_results = [
        {"coupon_id": "mt-repeat", "name": "美团测试券", "status": "already_claimed"},
        {"coupon_id": "tb-repeat", "name": "淘宝测试券", "status": "success"},
    ]
    try:
        record_claims(
            db, device, None, first_trace, first_results, app_env="dev"
        )
        record_claims(
            db, device, None, second_trace, second_results, app_env="prod"
        )

        assets = db.execute(
            select(CouponClaimRecord).where(CouponClaimRecord.device_id == device)
        ).scalars().all()
        assert len(assets) == 2
        assert {row.trace_id for row in assets} == {first_trace}

        events = db.execute(
            select(CouponClaimEvent).where(CouponClaimEvent.device_id == device)
        ).scalars().all()
        assert len(events) == 4
        assert {row.trace_id for row in events} == {first_trace, second_trace}

        scores = _point_scores_by_trace(db, [first_trace, second_trace])
        assert scores[first_trace] == {"succeeded": 1, "tried": 2}
        assert scores[second_trace] == {"succeeded": 2, "tried": 2}
        assert [row["status"] for row in coupon_point_details(
            db, trace_id=second_trace
        )] == ["already_claimed", "success"]
    finally:
        db.execute(delete(CouponClaimEvent).where(CouponClaimEvent.device_id == device))
        db.execute(delete(CouponClaimRecord).where(CouponClaimRecord.device_id == device))
        db.commit()
        db.close()
