"""
Flight/gruppe-scoping i ScoreTab (ADR-099, 2026-08-21). list_round_participants
speiler nå ut tournament_round_group_id + is_self per rad, slik at frontend kan
la score-føringsskjermen default til brukerens egen utslagsgruppe i stedet for
hele feltet. is_self beregnes server-side (ikke rå user_id, samme is_mine-
mønster som rounds.py) for ikke å lekke andre spilleres kontokobling.
"""

from app.auth import CurrentUser
from app.routers.individual_tournaments import (
    RoundGroupIn,
    SaveRoundGroupsIn,
    list_round_participants,
    save_round_groups,
)

from tests.conftest import (
    create_org,
    create_course,
    create_player,
    create_tee,
    create_tournament,
    create_tournament_participant,
    create_tournament_round,
    create_tournament_round_participant,
    create_user,
)

import app.db as app_db


async def _setup(org_id):
    tournament_id = await create_tournament(org_id, name="Flight-scoping-turnering")
    course_id = await create_course(org_id, name="Flight Links")
    tee_id = await create_tee(org_id, course_id, name="Gul")
    async with app_db.org_connection(org_id) as conn:
        await conn.execute(
            "UPDATE tournament SET format_type = 'individual', scoring_method = 'stroke_gross' WHERE id = $1",
            tournament_id,
        )
    round_id = await create_tournament_round(org_id, tournament_id, course_id, sequence=1)
    return tournament_id, round_id, tee_id


async def test_is_self_and_group_id_reflect_viewer_and_grouping(pool):
    org_id = await create_org()
    tournament_id, round_id, tee_id = await _setup(org_id)

    me_user_id = await create_user()
    other_user_id = await create_user()

    me_player_id = await create_player(org_id, display_name="Meg Selv", user_id=me_user_id)
    other_player_id = await create_player(org_id, display_name="En Annen", user_id=other_user_id)

    me_tp_id = await create_tournament_participant(org_id, tournament_id, me_player_id)
    other_tp_id = await create_tournament_participant(org_id, tournament_id, other_player_id)

    me_rp_id = await create_tournament_round_participant(org_id, round_id, me_tp_id, tee_id)
    other_rp_id = await create_tournament_round_participant(org_id, round_id, other_tp_id, tee_id)

    # Ingen gruppe satt ennå -- begge rader skal ha tournament_round_group_id=None.
    ungrouped = await list_round_participants(
        tournament_id, round_id, organization_id=org_id, user=CurrentUser(user_id=me_user_id),
    )
    by_id = {rp.id: rp for rp in ungrouped}
    assert by_id[me_rp_id].tournament_round_group_id is None
    assert by_id[other_rp_id].tournament_round_group_id is None
    assert by_id[me_rp_id].is_self is True
    assert by_id[other_rp_id].is_self is False

    # Grupper begge i samme gruppe -- gruppe-id skal nå matche på begge rader,
    # og is_self skal fortsatt kun være sann for den innloggede brukeren.
    await save_round_groups(
        tournament_id, round_id,
        SaveRoundGroupsIn(groups=[RoundGroupIn(sequence=1, round_participant_ids=[me_rp_id, other_rp_id])]),
        organization_id=org_id,
    )
    grouped = await list_round_participants(
        tournament_id, round_id, organization_id=org_id, user=CurrentUser(user_id=me_user_id),
    )
    by_id = {rp.id: rp for rp in grouped}
    assert by_id[me_rp_id].tournament_round_group_id is not None
    assert by_id[me_rp_id].tournament_round_group_id == by_id[other_rp_id].tournament_round_group_id
    assert by_id[me_rp_id].is_self is True
    assert by_id[other_rp_id].is_self is False

    # Sett fra den ANDRE brukerens perspektiv: is_self bytter side.
    as_other = await list_round_participants(
        tournament_id, round_id, organization_id=org_id, user=CurrentUser(user_id=other_user_id),
    )
    by_id = {rp.id: rp for rp in as_other}
    assert by_id[me_rp_id].is_self is False
    assert by_id[other_rp_id].is_self is True
