from app.main.data_model.games.stratedge.se_result import StratEdgeResult
from app.main.data_model.games.stratedge.se_round import SERound
from app.main.data_model.games.stratedge.se_round_scenario import SERoundScenario
from app.main.util import helpers


class StratEdgeResultRepository:

    def __init__(self, session):
        self.session = session

    def get_stratedge_results_by_round_name_and_game_session_id(self, round_name, game_session_id):
        stratedge_results = self.session.query(StratEdgeResult).join(SERoundScenario).join(SERound).\
            filter(StratEdgeResult.round_scenario_id == SERoundScenario.id).filter(SERoundScenario.round_id == SERound.id).\
            filter(SERound.game_session_id == game_session_id).filter(SERound.name == round_name)\
            .filter(SERoundScenario.is_decision == 1).filter(SERoundScenario.owner_team_id.is_(None)).all()
        if len(stratedge_results) == 0:
            return None
        return stratedge_results

    def delete_result_by_round_scenario(self, round_scenario):
        results = self.session.query(StratEdgeResult).filter(
            StratEdgeResult.round_scenario_id == round_scenario.id
        ).all()
        if results is None:
            return
        helpers.delete(self.session, results)


    def delete_result_by_result_id(self, result_id):
        results = self.session.query(StratEdgeResult).filter(StratEdgeResult.id == result_id).all
        helpers.delete(self.session , results)




