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 print_progress(current, total, prefix="", bar_length=40):
    if total == 0:
        return
    pct = current / total
    filled = int(bar_length * pct)
    bar = "█" * filled + "░" * (bar_length - filled)
    sys.stdout.write(f"\r  {prefix} [{bar}] {current}/{total} ({pct*100:.0f}%)")
    sys.stdout.flush()
    if current == total:
        sys.stdout.write("\n")


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, file_idx, total_files):
    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"  [{file_idx}/{total_files}] No keywords in {os.path.basename(txt_path)}, skipping")
        return

    total_kw = len(keywords)
    prefix = f"[{file_idx}/{total_files}] {os.path.basename(txt_path)}"

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


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

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

    for i, txt_path in enumerate(txt_files, 1):
        convert_file(txt_path, i, total_files)
        print_progress(i, total_files, prefix="Overall")

    print()
    logger.info("All files converted!")


if __name__ == "__main__":
    main()
