import os
import sys
import glob
import logging

SCRIPTS_DIR = os.path.dirname(os.path.abspath(__file__))
ROOT_DIR = os.path.dirname(SCRIPTS_DIR)

if ROOT_DIR not in sys.path:
    sys.path.insert(0, ROOT_DIR)

from pinscrape.database import create_database, append_keywords

logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s [%(levelname)s] %(message)s",
    datefmt="%H:%M:%S"
)
logger = logging.getLogger(__name__)


def find_keyword_files():
    kw_dir = os.path.join(ROOT_DIR, "keywords")
    if not os.path.exists(kw_dir):
        return []
    return sorted(glob.glob(os.path.join(kw_dir, "*.txt")))


def convert_file(txt_path):
    base_name = os.path.splitext(os.path.basename(txt_path))[0]
    db_dir = os.path.join(ROOT_DIR, "database")
    os.makedirs(db_dir, exist_ok=True)
    db_path = os.path.join(db_dir, f"{base_name}.sqlite")

    with open(txt_path, "r", encoding="utf-8") as f:
        keywords = [line.strip() for line in f if line.strip()]

    if not keywords:
        logger.warning(f"  No keywords in {os.path.basename(txt_path)}, skipping")
        return

    if os.path.exists(db_path):
        added = append_keywords(db_path, keywords)
        logger.info(f"  {os.path.basename(txt_path)} -> {os.path.basename(db_path)} (added {added} new keywords)")
    else:
        create_database(db_path, keywords)
        logger.info(f"  {os.path.basename(txt_path)} -> {os.path.basename(db_path)} ({len(keywords)} keywords)")


def main():
    txt_files = find_keyword_files()
    if not txt_files:
        logger.warning("No .txt files found in keywords/ folder")
        return

    logger.info(f"Found {len(txt_files)} keyword file(s)")

    for txt_path in txt_files:
        convert_file(txt_path)

    logger.info("All files converted!")


if __name__ == "__main__":
    main()
