import os
import json
import hashlib
from pathlib import Path
from datetime import datetime

BASE_DIR = Path("/home/dayhanbiz/public_html/Библиотека 3-DAMJA")
CATALOG_DIR = BASE_DIR / "catalog"
CATALOG_DIR.mkdir(parents=True, exist_ok=True)

# Новые пути к единому хранилищу
STORAGE_DIR = Path("/home/dayhanbiz/public_html/3-damja.science/storage")

HISTORY_SOURCES = [
    STORAGE_DIR / "history_books",
    STORAGE_DIR / "history_pdf",
    STORAGE_DIR / "history_vostlit"
]

VAM_SOURCES = [
    STORAGE_DIR / "water_vam"
]

SUPPORTED_EXTENSIONS = {'.pdf', '.djvu', '.txt', '.doc', '.docx', '.html', '.htm'}

def calculate_file_hash(file_path: Path) -> str:
    sha256_hash = hashlib.sha256()
    try:
        with open(file_path, "rb") as f:
            for byte_block in iter(lambda: f.read(65536), b""):
                sha256_hash.update(byte_block)
        return sha256_hash.hexdigest()
    except Exception as e:
        print(f"Ошибка чтения файла {file_path}: {e}")
        return ""

def scan_sources():
    print("=== Запуск сканирования и каталогизации источников ===")
    print(f"Время: {datetime.now()}\n")

    seen_hashes = {}
    catalog_history = []
    catalog_vam = []

    def process_folder(source_path: Path, category: str):
        if not source_path.exists():
            print(f"⚠️ Директория не найдена: {source_path}")
            return

        print(f"Сканирование [{category}]: {source_path.name}")
        count = 0

        for file_path in source_path.rglob("*"):
            if file_path.is_file() and file_path.suffix.lower() in SUPPORTED_EXTENSIONS:
                file_hash = calculate_file_hash(file_path)
                if not file_hash:
                    continue

                file_info = {
                    "path": str(file_path),
                    "name": file_path.name,
                    "extension": file_path.suffix.lower(),
                    "size_bytes": file_path.stat().st_size,
                    "modified": file_path.stat().st_mtime,
                    "hash": file_hash,
                    "categories": [category]
                }

                if file_hash in seen_hashes:
                    existing = seen_hashes[file_hash]
                    if category not in existing["categories"]:
                        existing["categories"].append(category)
                        print(f"🔗 Найдена связь между темами для файла: {file_path.name}")
                    continue

                seen_hashes[file_hash] = file_info

                if category == "history":
                    catalog_history.append(file_info)
                elif category == "vam":
                    catalog_vam.append(file_info)

                count += 1

        print(f"   → Найдено уникальных файлов: {count}\n")

    for src in HISTORY_SOURCES:
        process_folder(src, "history")

    for src in VAM_SOURCES:
        process_folder(src, "vam")

    history_file = CATALOG_DIR / "list_history.json"
    vam_file = CATALOG_DIR / "list_vam.json"

    with open(history_file, "w", encoding="utf-8") as f:
        json.dump(catalog_history, f, ensure_ascii=False, indent=4)

    with open(vam_file, "w", encoding="utf-8") as f:
        json.dump(catalog_vam, f, ensure_ascii=False, indent=4)

    print("=== Сканирование завершено ===")
    print(f"Сохранено в историю: {len(catalog_history)} файлов -> {history_file}")
    print(f"Сохранено в ВАМ: {len(catalog_vam)} файлов -> {vam_file}")
    print(f"Всего уникальных документов с учетом дедупликации: {len(seen_hashes)}")

if __name__ == "__main__":
    scan_sources()
