from datetime import datetime, timedelta, timezone

from app.admin.repositories import queries
from app.db.session import SessionLocal
from app.models.comparison import ComparisonRecord
from app.models.feedback import Feedback
from app.repositories import user as user_repo


def test_feedback_device_details_use_same_user_and_model() -> None:
    with SessionLocal() as db:
        submitted_at = datetime.now(timezone.utc)
        user = user_repo.upsert_user_for_login(
            db,
            phone="13800009876",
            register_channel="sms",
        )
        db.add_all(
            [
                ComparisonRecord(
                    user_id=user.id,
                    trace_id="feedback-device-v2166ba",
                    device_model="V2166BA",
                    device_manufacturer="vivo",
                    rom_name="OriginOS",
                    rom_version=13,
                    created_at=submitted_at - timedelta(minutes=5),
                ),
                ComparisonRecord(
                    user_id=user.id,
                    trace_id="feedback-device-other",
                    device_model="OTHER-CODE",
                    device_manufacturer="Other",
                    rom_name="OtherOS",
                    rom_version=99,
                    created_at=submitted_at - timedelta(minutes=5),
                ),
                ComparisonRecord(
                    user_id=user.id,
                    trace_id="feedback-device-future-upgrade",
                    device_model="V2166BA",
                    device_manufacturer="vivo-new",
                    rom_name="OriginOS",
                    rom_version=99,
                    created_at=submitted_at + timedelta(minutes=5),
                ),
                Feedback(
                    user_id=user.id,
                    content="设备信息补全测试",
                    contact="",
                    status="pending",
                    device_model="V2166BA",
                    rom_name="OriginOS",
                    android_version="13",
                    created_at=submitted_at,
                ),
            ]
        )
        db.commit()

        items, _next_cursor, total = queries.list_feedbacks(
            db,
            user_id=user.id,
            limit=20,
        )

        assert total == 1
        assert items[0].device_model == "V2166BA"
        assert items[0].device_model_name == "vivo Y77e"
        assert items[0].device_manufacturer == "vivo"
        assert items[0].rom_version == 13
