"""
PySide6 GUI for Kontor
"""
import sys
import logging.config
from pathlib import Path
from platformdirs import PlatformDirs
from PySide6.QtWidgets import QApplication
import yaml
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from kontor_schema.base import Base

from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
from gui.main_window import MainWindow

parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter)
parser.add_argument('--verbose', '-v', action='count', default=0)
parser.add_argument('--config', '-c', default='kontor-docker')
args = parser.parse_args()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    dirs = PlatformDirs(args.config)
    database_config = Path(dirs.user_config_dir, 'database-config.yaml')
    with open(database_config, 'rt') as f:
        db_config = yaml.safe_load(f.read())
    connect_string = ('mariadb+mariadbconnector://{}:{}@{}:{}/{}'.format(
        db_config['mariadb']['user'],
        db_config['mariadb']['password'],
        db_config['mariadb']['host'],
        db_config['mariadb']['port'],
        db_config['mariadb']['database']
    ))
    logging_config = Path(dirs.user_config_dir, 'logging-config.yaml')
    with open(logging_config, 'rt') as f:
        config = yaml.safe_load(f.read())
    logging.config.dictConfig(config)
    logger = logging.getLogger('development')
    # engine = create_engine(connect_string, echo=True)
    engine = create_engine(connect_string)
    Base.metadata.create_all(bind=engine, checkfirst=True)
    __session__ = sessionmaker(bind=engine)

    window = MainWindow(engine, logger)
    window.show()
    app.exec()
