"""
import data from json file to MariaDB
"""
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
import json
import os
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from db.models import registry
from db.models.base import Base
from config import get_logger

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

DB_USER: str = os.getenv("DB_USER", "kontor")
DB_PASSWORD: str = os.getenv("DB_PASSWORD", "kontor")
DB_SERVER: str = os.getenv("DB_SERVER", "127.0.0.1")
DB_PORT: int = int(os.getenv("DB_PORT", 5432))
DB_DBNAME: str = os.getenv("DB_DBNAME", "kontor")
DATABASE_URL: str = f"postgresql://{DB_USER}:{DB_PASSWORD}@{DB_SERVER}:{DB_PORT}/{DB_DBNAME}"

if __name__ == '__main__':
    logger = get_logger(args.verbose, args.config)
    logger.info('kontor.export started')
    engine = create_engine(DATABASE_URL)
    Base.metadata.create_all(bind=engine, checkfirst=True)
    SessionLocal = sessionmaker(bind=engine)
    with SessionLocal() as db:
        data = {}
        tables = registry.keys()
        for table in tables:
            # logger.info(f"Table {table.name} with {table.id}")
            model = registry[table]
            rows = db.query(model).all()
            entries = []
            for row in rows:
                entry = row.export_dict()
                entries.append(entry)
            data[table] = entries
            logger.info(f"{table}: {len(entries)} exported")
        try:
            json_dump = json.dumps(data, indent=4)
            with open(args.file, "w") as dump_file:
                dump_file.write(json_dump)
        except TypeError as error:
            logger.info(f"{error}")
        logger.info(f"{len(data)} tables exported")
    #kontor_db = KontorDB(engine, logger)
    #kontor_db.export_db(ExportType.JSON, args.file)
    logger.info('kontor.export finished')

