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

Technology: Business Objects
Feature:    MTG Game Round Business Object
"""

# internal
from business_objects.base import Base
from business_objects.db_base import SQLAlchemy_ABC, Get_Many_Parameters_Base
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


class MTG_Game_Round(SQLAlchemy_ABC, Base):
    ATTR_ROUND_ID: ClassVar[str] = 'round_id'
    ATTR_GAME_ID: ClassVar[str] = 'game_id'
    FLAG_ROUND: ClassVar[str] = 'round'
    FLAG_NOTES: ClassVar[str] = 'notes'
    NAME_ATTR_OPTION_VALUE: ClassVar[str] = ATTR_ROUND_ID
    NAME_ATTR_OPTION_TEXT: ClassVar[str] = ATTR_ROUND_ID

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

    round_id = db.Column(db.Integer, primary_key=True)
    game_id = db.Column(db.Integer)
    notes = db.Column(db.Text)
    display_order = db.Column(db.Integer)
    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.round_id = 0
        self.is_new = False
        super().__init__()

    @classmethod
    def from_db_mtg_game_round(cls, query_row):
        _m = f'{cls.__qualname__}.from_db_mtg_game_round'
        round = cls()
        round.round_id = query_row[0]
        round.game_id = query_row[1]
        round.notes = query_row[2]
        round.display_order = query_row[3]
        round.active = av.input_bool(query_row[4], cls.FLAG_ACTIVE, _m)
        round.created_on = query_row[5]
        round.created_by_user_id = query_row[6]
        return round

    @classmethod
    def from_json(cls, json):
        _m = f'{cls.__qualname__}.from_json'
        round = cls()
        if json is None: return round
        round.round_id = json.get(cls.ATTR_ROUND_ID, -1)
        round.game_id = json.get(cls.ATTR_GAME_ID, None)
        round.notes = json.get(cls.FLAG_NOTES, None)
        round.display_order = json.get(cls.FLAG_DISPLAY_ORDER, -1)
        round.active = av.input_bool(json.get(cls.FLAG_ACTIVE, True), cls.FLAG_ACTIVE, _m)
        round.created_on = json.get(cls.FLAG_CREATED_ON, None)
        round.created_by_user_id = json.get(Base.ATTR_USER_ID, None)
        return round

    def to_json(self):
        as_json = {
            **self.get_shared_json_attributes(self)
            , self.ATTR_ROUND_ID: self.round_id
            , self.ATTR_GAME_ID: self.game_id
            , self.FLAG_NOTES: self.notes
            , self.FLAG_DISPLAY_ORDER: self.display_order
            , self.FLAG_ACTIVE: self.active
            , self.FLAG_CREATED_ON: self.created_on
            , Base.ATTR_USER_ID: self.created_by_user_id
        }
        return as_json

    def __repr__(self):
        return f'''
{self.__class__.__name__}(
    {self.ATTR_ROUND_ID}: {self.round_id}
    {self.ATTR_GAME_ID}: {self.game_id}
    {self.FLAG_NOTES}: {self.notes}
    {self.FLAG_DISPLAY_ORDER}: {self.display_order}
    {self.FLAG_ACTIVE}: {self.active}
    {self.FLAG_CREATED_ON}: {self.created_on}
    {Base.ATTR_USER_ID}: {self.created_by_user_id}
)
            '''

class MTG_Game_Round_Temp(db.Model, Base):
    __tablename__ = 'tcg_mtg_game_round_temp'
    __table_args__ = { 'extend_existing': True }
    temp_id = db.Column(db.Integer, primary_key=True)
    round_id = db.Column(db.Integer)
    game_id = db.Column(db.Integer)
    notes = db.Column(db.Text)
    display_order = db.Column(db.Integer)
    active = db.Column(db.Boolean)
    created_on = db.Column(db.DateTime)
    guid = db.Column(Uuid)

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

    @classmethod
    def from_round(cls, round, guid):
        _m = 'MTG_Game_Round_Temp.from_round'
        temp = cls()
        temp.round_id = round.round_id
        temp.game_id = round.game_id
        temp.notes = round.notes
        temp.display_order = round.display_order
        temp.active = round.active
        temp.created_on = round.created_on
        temp.guid = guid
        return temp


class Parameters_MTG_Game_Round(Get_Many_Parameters_Base):
    get_all_game: bool
    get_inactive_game: bool
    game_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_all_game = True
            , get_inactive_game = False
            , game_ids = ''
            , require_all_id_filters_met = True
            , require_any_id_filters_met = True
        )

    @classmethod
    def from_json(cls, json):
        return cls(
              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', '')
            , 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_all_game': self.get_all_game
            , 'a_get_inactive_game': self.get_inactive_game
            , 'a_game_ids': self.game_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_all_game': Boolean
            , 'a_get_inactive_game': Boolean
            , 'a_game_ids': Text
            , 'a_require_all_id_filters_met': Boolean
            , 'a_require_any_id_filters_met': Boolean
        }
