import os
import sqlite3
import json
from pathlib import Path

BASE_DIR = Path("/home/dayhanbiz/public_html/3-damja.science")
CATALOG_DIR = Path("/home/dayhanbiz/public_html/Библиотека 3-DAMJA/catalog")
TEXTS_DIR = BASE_DIR / "extracted_texts"
DB_PATH = BASE_DIR / "search_databases" / "library_search.db"

def init_db():
    DB_PATH.parent.mkdir(parents=True, exist_ok=True)
    conn = sqlite3.connect(DB_PATH)
    cursor = conn.cursor()
    
    cursor.execute("""
        CREATE TABLE IF NOT EXISTS documents (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            hash_name TEXT UNIQUE,
            title TEXT,
            original_path TEXT
        )
    """)
    
    cursor.execute("""
        CREATE VIRTUAL TABLE IF NOT EXISTS pages_fts USING fts5(
            book_hash,
            page_number,
            content,
            tokenize='unicode61'
        )
    """)
    
    conn.commit()
    conn.close()

def load_file_metadata():
    metadata = {}
    for json_name in ["list_history.json", "list_vam.json"]:
        j_path = CATALOG_DIR / json_name
        if j_path.exists():
            try:
                with open(j_path, "r", encoding="utf-8") as f:
                    items = json.load(f)
                    for item in items:
                        h = item.get("hash")
                        if h:
                            metadata[h] = {
                                "name": item.get("name"),
                                "path": item.get("path")
                            }
            except Exception as e:
                print(f"Ошибка чтения {json_name}: {e}")
    return metadata

def build_index():
    init_db()
    file_meta = load_file_metadata()
    
    conn = sqlite3.connect(DB_PATH)
    cursor = conn.cursor()
    
    print("=== Запуск индексации накопленных текстов ===")
    
    if not TEXTS_DIR.exists():
        print("Папка с текстами пока пуста.")
        conn.close()
        return

    book_dirs = [d for d in TEXTS_DIR.iterdir() if d.is_dir()]
    indexed_books = 0
    
    for book_dir in book_dirs:
        book_hash = book_dir.name
        
        meta = file_meta.get(book_hash, {})
        title = meta.get("name", book_hash)
        orig_path = meta.get("path", "")
        
        cursor.execute("SELECT 1 FROM documents WHERE hash_name = ?", (book_hash,))
        if cursor.fetchone():
            cursor.execute("UPDATE documents SET title = ?, original_path = ? WHERE hash_name = ?", (title, orig_path, book_hash))
            continue
            
        cursor.execute("INSERT OR IGNORE INTO documents (hash_name, title, original_path) VALUES (?, ?, ?)", (book_hash, title, orig_path))
        
        page_files = sorted(book_dir.glob("page_*.txt"))
        for p_file in page_files:
            try:
                page_num_str = p_file.stem.split('_')[1]
                page_num = int(page_num_str)
                
                with open(p_file, "r", encoding="utf-8", errors="ignore") as f:
                    content = f.read()
                    
                if content.strip():
                    cursor.execute(
                        "INSERT INTO pages_fts (book_hash, page_number, content) VALUES (?, ?, ?)",
                        (book_hash, page_num, content)
                    )
            except Exception as e:
                continue
                
        indexed_books += 1
        if indexed_books % 20 == 0:
            conn.commit()
            print(f"   → Проиндексировано новых книг: {indexed_books}")
            
    conn.commit()
    conn.close()
    print(f"✅ Индексация завершена. Всего новых книг обработано: {indexed_books}")

if __name__ == "__main__":
    build_index()
