"""
Project:    PARTS Website
Author:     Edward Middleton-Smith
            Precision And Research Technology Systems Limited

Technology: Business Objects
Feature:    MTG Game Session Member Business Object
"""

# internal
from business_objects.base import Base
from business_objects.db_base import SQLAlchemy_ABC, Get_Many_Parameters_Base
from business_objects.tcg.mtg_game import MTG_Game
from business_objects.tcg.user import User
import lib.argument_validation as av
from extensions import db
from helpers.helper_app import Helper_App
# external
from dataclasses import dataclass
from typing import ClassVar
from sqlalchemy import Uuid
from sqlalchemy.types import Text, Boolean
# import uuid

class MTG_Game_Session_Member(SQLAlchemy_ABC, Base):
    ATTR_SESSION_MEMBER_ID: ClassVar[str] = 'member_id'
    FLAG_IS_PERMITTED: ClassVar[str] = 'is_permitted'
    NAME_ATTR_OPTION_VALUE: ClassVar[str] = ATTR_SESSION_MEMBER_ID
    NAME_ATTR_OPTION_TEXT: ClassVar[str] = Base.FLAG_NAME

    __tablename__ = 'tcg_mtg_game_session_member'
    __table_args__ = { 'extend_existing': True }

    member_id = db.Column(db.Integer, primary_key=True)
    game_id = db.Column(db.Integer)
    user_id = db.Column(db.Integer)
    is_permitted = db.Column(db.Boolean)
    active = db.Column(db.Boolean)
    created_on = db.Column(db.DateTime)
    created_by_user_id = db.Column(db.Integer)
    updated_last_on = db.Column(db.DateTime)
    updated_last_by_user_id = db.Column(db.Integer)
    change_set_id = db.Column(db.Integer)

    def __init__(self):
        self.member_id = 0
        self.is_new = False
        super().__init__()

    @classmethod
    def from_db_mtg_game_player(cls, query_row):
        _m = f'{cls.__qualname__}.from_db_mtg_game_player'
        player = cls()
        player.member_id = query_row[0]
        player.game_id = query_row[1]
        player.user_id = query_row[2]
        player.is_permitted = query_row[3]
        player.active = av.input_bool(query_row[4], cls.FLAG_ACTIVE, _m)
        player.created_on = query_row[5]
        player.created_by_user_id = query_row[6]
        return player

    @classmethod
    def from_json(cls, json):
        _m = f'{cls.__qualname__}.from_json'
        player = cls()
        if json is None: return player
        player.member_id = json.get(cls.ATTR_SESSION_MEMBER_ID, -1)
        player.game_id = json.get(MTG_Game.ATTR_GAME_ID, None)
        player.user_id = json.get(Base.ATTR_USER_ID, None)
        player.is_permitted = json.get(cls.FLAG_IS_PERMITTED, None)
        player.active = av.input_bool(json.get(cls.FLAG_ACTIVE, True), cls.FLAG_ACTIVE, _m)
        player.created_on = json.get(cls.FLAG_CREATED_ON, None)
        player.created_by_user_id = json.get(Base.ATTR_USER_ID, None)
        return player

    def to_json(self):
        as_json = {
            **self.get_shared_json_attributes(self)
            , self.ATTR_SESSION_MEMBER_ID: self.member_id
            , MTG_Game.ATTR_GAME_ID: self.game_id
            , Base.ATTR_USER_ID: self.user_id
            , self.FLAG_IS_PERMITTED: self.is_permitted
            , self.FLAG_ACTIVE: self.active
            , self.FLAG_CREATED_ON: self.created_on
        }
        return as_json

    def __repr__(self):
        return f'''
{self.__class__.__name__}(
    {self.ATTR_SESSION_MEMBER_ID}: {self.member_id}
    {MTG_Game.ATTR_GAME_ID}: {self.game_id}
    {Base.ATTR_USER_ID}: {self.user_id}
    {self.FLAG_IS_PERMITTED}: {self.is_permitted}
    {self.FLAG_ACTIVE}: {self.active}
    {self.FLAG_CREATED_ON}: {self.created_on}
)
            '''

class MTG_Game_Session_Member_Temp(db.Model, Base):
    __tablename__ = 'tcg_mtg_game_session_member_temp'
    __table_args__ = { 'extend_existing': True }
    temp_id = db.Column(db.Integer, primary_key=True)
    member_id = db.Column(db.Integer)
    game_id = db.Column(db.Integer)
    user_id = db.Column(db.Integer)
    is_permitted = db.Column(db.Boolean)
    active = db.Column(db.Boolean)
    created_on = db.Column(db.DateTime)
    guid = db.Column(Uuid) #, default = uuid.uuid4)

    def __init__(self):
        super().__init__()

    @classmethod
    def from_session_member(cls, member, guid):
        _m = 'MTG_Game_Session_Member_Temp.from_session_member'
        temp = cls()
        temp.member_id = member.member_id
        temp.game_id = member.game_id
        temp.user_id = member.user_id
        temp.is_permitted = member.is_permitted
        temp.active = member.active
        temp.created_on = member.created_on
        temp.guid = guid
        return temp


class Parameters_MTG_Game_Session_Member(Get_Many_Parameters_Base):
    get_inactive_session_member: bool
    get_all_game: bool
    get_inactive_game: bool
    game_ids: str
    get_all_user: bool
    get_inactive_user: bool
    user_ids: str
    require_all_id_filters_met: bool
    require_any_id_filters_met: bool

    @classmethod
    def get_default(cls, user_id_session):
        return cls(
              get_inactive_session_member = False
            , get_all_game = True
            , get_inactive_game = False
            , game_ids = ''
            , get_all_user = True
            , get_inactive_user = False
            , user_ids = str(user_id_session)
            , require_all_id_filters_met = True
            , require_any_id_filters_met = True
        )

    @classmethod
    def from_json(cls, json):
        return cls(
              get_inactive_session_member = json.get('a_get_inactive_session_member', False)
            , get_all_game = json.get('a_get_all_game', False)
            , get_inactive_game = json.get('a_get_inactive_game', False)
            , game_ids = json.get('a_game_ids', '')
            , get_all_user = json.get('a_get_all_user', False)
            , get_inactive_user = json.get('a_get_inactive_user', False)
            , user_ids = json.get('a_user_ids', '')
            , require_all_id_filters_met = json.get('a_require_all_id_filters_met', True)
            , require_any_id_filters_met = json.get('a_require_any_id_filters_met', True)
        )

    def to_json(self):
        return {
              'a_get_inactive_session_member': self.get_inactive_session_member
            , 'a_get_all_game': self.get_all_game
            , 'a_get_inactive_game': self.get_inactive_game
            , 'a_game_ids': self.game_ids
            , 'a_get_all_user': self.get_all_user
            , 'a_get_inactive_user': self.get_inactive_user
            , 'a_user_ids': self.user_ids
            , 'a_require_all_id_filters_met': self.require_all_id_filters_met
            , 'a_require_any_id_filters_met': self.require_any_id_filters_met
        }

    @staticmethod
    def get_type_hints():
        return {
              'a_get_inactive_session_member': Boolean
            , 'a_get_all_game': Boolean
            , 'a_get_inactive_game': Boolean
            , 'a_game_ids': Text
            , 'a_get_all_user': Boolean
            , 'a_get_inactive_user': Boolean
            , 'a_user_ids': Text
            , 'a_require_all_id_filters_met': Boolean
            , 'a_require_any_id_filters_met': Boolean
        }
