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

Technology: Business Objects
Feature:    MTG Deck Commander Bracket 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


class MTG_Deck_Commander_Bracket(SQLAlchemy_ABC, Base):
    ATTR_COMMANDER_BRACKET_ID: ClassVar[str] = 'commander_bracket_id'
    FLAG_COMMANDER_BRACKET: ClassVar[str] = 'commander_bracket'
    FLAG_DESCRIPTION: ClassVar[str] = 'description'
    FLAG_DISPLAY_ORDER: ClassVar[str] = 'display_order'
    NAME_ATTR_OPTION_VALUE: ClassVar[str] = ATTR_COMMANDER_BRACKET_ID
    NAME_ATTR_OPTION_TEXT: ClassVar[str] = Base.FLAG_NAME

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

    commander_bracket_id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.Text)
    description = 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.commander_bracket_id = 0
        self.is_new = False
        super().__init__()

    @classmethod
    def from_db_mtg_deck_commander_bracket(cls, query_row):
        _m = f'{cls.__qualname__}.from_db_mtg_deck_commander_bracket'
        bracket = cls()
        bracket.commander_bracket_id = query_row[0]
        bracket.name = query_row[1]
        bracket.description = query_row[2]
        bracket.display_order = query_row[3]
        bracket.active = av.input_bool(query_row[4], cls.FLAG_ACTIVE, _m)
        bracket.created_on = query_row[5]
        bracket.created_by_user_id = query_row[6]
        return bracket

    @classmethod
    def from_json(cls, json):
        _m = f'{cls.__qualname__}.from_json'
        bracket = cls()
        if json is None: return bracket
        bracket.commander_bracket_id = json.get(cls.ATTR_COMMANDER_BRACKET_ID, -1)
        bracket.name = json.get(cls.FLAG_NAME, None)
        bracket.description = json.get(cls.FLAG_DESCRIPTION, None)
        bracket.display_order = json.get(cls.FLAG_DISPLAY_ORDER, 0)
        bracket.active = av.input_bool(json.get(cls.FLAG_ACTIVE, True), cls.FLAG_ACTIVE, _m)
        bracket.created_on = json.get(cls.FLAG_CREATED_ON, None)
        bracket.created_by_user_id = json.get(Base.ATTR_USER_ID, None)
        return bracket

    def to_json(self):
        as_json = {
            **self.get_shared_json_attributes(self)
            , self.ATTR_COMMANDER_BRACKET_ID: self.commander_bracket_id
            , self.FLAG_NAME: self.name
            , self.FLAG_DESCRIPTION: self.description
            , 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_COMMANDER_BRACKET_ID}: {self.commander_bracket_id}
    {self.FLAG_NAME}: {self.name}
    {self.FLAG_DESCRIPTION}: {self.description}
    {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 Parameters_MTG_Deck_Commander_Bracket(Get_Many_Parameters_Base):
    get_all_commander_bracket: bool
    get_inactive_commander_bracket: bool
    commander_bracket_ids: str
    commander_bracket_names: str
    require_all_id_filters_met: bool
    require_any_id_filters_met: bool
    require_all_non_id_filters_met: bool
    require_any_non_id_filters_met: bool

    @classmethod
    def get_default(cls):
        return cls(
              get_all_commander_bracket = True
            , get_inactive_commander_bracket = False
            , commander_bracket_ids = ''
            , commander_bracket_names = ''
            , require_all_id_filters_met = False
            , require_any_id_filters_met = True
            , require_all_non_id_filters_met = False
            , require_any_non_id_filters_met = True
        )

    @classmethod
    def from_json(cls, json):
        return cls(
              get_all_commander_bracket = json.get('a_get_all_commander_bracket', True)
            , get_inactive_commander_bracket = json.get('a_get_inactive_commander_bracket', False)
            , commander_bracket_ids = json.get('a_commander_bracket_ids', '')
            , commander_bracket_names = json.get('a_commander_bracket_names', '')
            , require_all_id_filters_met = json.get('a_require_all_id_filters_met', False)
            , require_any_id_filters_met = json.get('a_require_any_id_filters_met', True)
            , require_all_non_id_filters_met = json.get('a_require_all_non_id_filters_met', False)
            , require_any_non_id_filters_met = json.get('a_require_any_non_id_filters_met', True)
        )

    def to_json(self):
        return {
              'a_get_all_commander_bracket': self.get_all_commander_bracket
            , 'a_get_inactive_commander_bracket': self.get_inactive_commander_bracket
            , 'a_commander_bracket_ids': self.commander_bracket_ids
            , 'a_commander_bracket_names': self.commander_bracket_names
            , 'a_require_all_id_filters_met': self.require_all_id_filters_met
            , 'a_require_any_id_filters_met': self.require_any_id_filters_met
            , 'a_require_all_non_id_filters_met': self.require_all_non_id_filters_met
            , 'a_require_any_non_id_filters_met': self.require_any_non_id_filters_met
        }
