from pathlib import Path

from mbetterclient.core.games_thread import GamesThread
from mbetterclient.core.message_bus import MessageBus
from mbetterclient.database.manager import DatabaseManager
from mbetterclient.database.models import BetDetailModel, MatchModel, MatchOutcomeModel


def _build_games_thread(tmp_path: Path):
    db_path = tmp_path / "settlement_consistency.db"
    db_manager = DatabaseManager(str(db_path))
    assert db_manager.initialize()
    message_bus = MessageBus()
    games_thread = GamesThread("test_games_thread", message_bus, db_manager)
    return games_thread, db_manager


def _seed_match(session):
    match = MatchModel(
        match_number=1,
        fighter1_township='A',
        fighter2_township='B',
        venue_kampala_township='Kampala',
        filename='fixture.txt',
        file_sha1sum='fixture-sha1',
        fixture_id='fixture-1',
        active_status=True,
        status='done',
        result='WIN1',
        under_over_result='UNDER',
        winning_outcomes='["WIN1", "KO1"]',
    )
    session.add(match)
    session.flush()

    session.add_all([
        MatchOutcomeModel(match_id=match.id, column_name='UNDER', float_value=2.0),
        MatchOutcomeModel(match_id=match.id, column_name='OVER', float_value=3.0),
        MatchOutcomeModel(match_id=match.id, column_name='WIN1', float_value=4.0),
        MatchOutcomeModel(match_id=match.id, column_name='KO1', float_value=5.0),
        MatchOutcomeModel(match_id=match.id, column_name='WIN2', float_value=6.0),
    ])

    session.add_all([
        BetDetailModel(bet_id='bet-under', match_id=match.id, match_number=1, outcome='UNDER', amount=10.0, result='pending'),
        BetDetailModel(bet_id='bet-over', match_id=match.id, match_number=1, outcome='OVER', amount=11.0, result='pending'),
        BetDetailModel(bet_id='bet-win1', match_id=match.id, match_number=1, outcome='WIN1', amount=12.0, result='pending'),
        BetDetailModel(bet_id='bet-ko1', match_id=match.id, match_number=1, outcome='KO1', amount=13.0, result='pending'),
        BetDetailModel(bet_id='bet-win2', match_id=match.id, match_number=1, outcome='WIN2', amount=14.0, result='pending'),
    ])
    session.commit()
    return match.id


def test_ensure_all_bets_resolved_settles_two_extraction_channels(tmp_path):
    games_thread, db_manager = _build_games_thread(tmp_path)
    session = db_manager.get_session()
    try:
        match_id = _seed_match(session)
    finally:
        session.close()

    games_thread._ensure_all_bets_resolved(match_id, 'WIN1')

    session = db_manager.get_session()
    try:
        rows = {
            row.outcome: row
            for row in session.query(BetDetailModel).filter_by(match_id=match_id).all()
        }
        assert rows['UNDER'].result == 'win'
        assert rows['UNDER'].win_amount == 20.0
        assert rows['OVER'].result == 'lost'
        assert rows['OVER'].win_amount == 0.0
        assert rows['WIN1'].result == 'win'
        assert rows['WIN1'].win_amount == 48.0
        assert rows['KO1'].result == 'win'
        assert rows['KO1'].win_amount == 65.0
        assert rows['WIN2'].result == 'lost'
        assert rows['WIN2'].win_amount == 0.0
        assert session.query(BetDetailModel).filter_by(match_id=match_id, result='pending').count() == 0
    finally:
        session.close()
        db_manager.close()
