from dataclasses import dataclass
import os
import shutil
import configparser
import logging
from datetime import datetime
from peewee import SqliteDatabase
from rich.logging import RichHandler

# first things first: logging
logging.basicConfig(
    format='%(message)s',
    level=logging.INFO,
    datefmt='%H:%M:%S', # add %Y-%m-%d if needed
    handlers=[RichHandler()]
    )
logger = logging.getLogger(__name__)


# load config file containing constants and secrets
parsed = configparser.ConfigParser()
parsed.read("/app/containerdata/config/news_fetch.config.ini")

if os.getenv("DEBUG", "false") == "true":
    logger.warning("Found 'DEBUG=true', setting up dummy databases")
    
    db_base_path = parsed["DATABASE"]["db_path_dev"]
    parsed["SLACK"]["archive_id"] = parsed["SLACK"]["debug_id"]
    parsed["MAIL"]["recipient"] = parsed["MAIL"]["sender"]
    parsed["DOWNLOADS"]["local_storage_path"] = parsed["DATABASE"]["db_path_dev"]
else:
    logger.warning("Found 'DEBUG=false' and running on production databases, I hope you know what you're doing...")
    db_base_path = parsed["DATABASE"]["db_path_prod"]
    logger.info("Backing up databases")
    backup_dst = parsed["DATABASE"]["db_backup"]
    today = datetime.today().strftime("%Y.%m.%d")
    shutil.copyfile(
        os.path.join(db_base_path, parsed["DATABASE"]["chat_db_name"]), 
        os.path.join(backup_dst, today + "." + parsed["DATABASE"]["chat_db_name"]), 
        )
    shutil.copyfile(
        os.path.join(db_base_path, parsed["DATABASE"]["download_db_name"]), 
        os.path.join(backup_dst, today + "." + parsed["DATABASE"]["download_db_name"]), 
        )


from utils_storage import models

# Set up the database
models.set_db(
    SqliteDatabase(
        os.path.join(db_base_path, parsed["DATABASE"]["chat_db_name"]),
        pragmas = {'journal_mode': 'wal'} # mutliple threads can read at once
    ),
    SqliteDatabase(
        os.path.join(db_base_path, parsed["DATABASE"]["download_db_name"]),
        pragmas = {'journal_mode': 'wal'} # mutliple threads can read at once
    )
)