import sqlite3
import logging
import os
from dotenv import dotenv_values

logger = logging.getLogger(__name__)

DB_DIR = "app_database"
DB_FILE = os.path.join(DB_DIR, "factum.db")

class DBService:
    def __init__(self):
        logging.debug("Initializing DBService")
        self.conn = self.connect_db()
        
    def connect_db(self):
        logging.debug(f"Trying to connect to database at {DB_FILE}")
        if not os.path.exists(DB_DIR):
            logging.debug(f"Database directory {DB_DIR} does not exist, creating...")
            os.makedirs(DB_DIR, exist_ok=True)
        try:
            conn = sqlite3.connect(DB_FILE)
            logging.info("Database connection established.")
            return conn
        except sqlite3.Error as e:
            logging.error(f"Database connection failed: {e}")
            raise