import asyncio
import sys
import os

sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../..')))

from langchain_community.document_loaders import TextLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_openai import OpenAIEmbeddings
from app.core.database import MongoDBClient
from app.core.config import config as Config
import uuid
from qdrant_client import QdrantClient
from qdrant_client.models import VectorParams, Distance, PointStruct

def load_all_text_files(directory):
    
    documents = []
    for root, dirs, files in os.walk(directory):
        print(f"Processing directory: {dirs}")
        documents = []
        for file in files:
            if file.endswith('.txt'):
                file_path = os.path.join(root, file)
                with open(file_path, 'r', encoding='utf-8') as f:
                    content = f.read().strip()

                    if 'PREGUNTA:' in content and 'RESPOSTA:' in content:
                        pregunta = content.split('RESPOSTA:')[0].replace('PREGUNTA:', '').strip()
                        resposta = content.split('RESPOSTA:')[1].strip()
                        documents.append({
                            "pregunta": pregunta,
                            "resposta": resposta
                        })
        if documents:
            collection_name = os.path.basename(root)
            print(f"Processant {len(documents)} documents per la col·lecció: {collection_name}")
            save_preguntes_mongo_qdrant(documents, collection_name)
    return documents

def chunkize():
    print(Config.DATA_PATH)
    documents = load_all_text_files(Config.DATA_PATH)
    

def init_qdrant_collection(collection_name, embedding_dim=1536):
    if Config.ENVIRONMENT == "production":
        qdrant = QdrantClient(
            url=Config.QDRANT_URI,
            api_key=Config.QDRANT_API_KEY
        )
    else:
        qdrant = QdrantClient(
            host=Config.QDRANT_URI,
            port=6333
        )

    if qdrant.collection_exists(collection_name):
        qdrant.delete_collection(collection_name=collection_name)
    
    if not qdrant.collection_exists(collection_name):
        qdrant.create_collection(
            collection_name=collection_name,
            vectors_config=VectorParams(
                size=embedding_dim,
                distance=Distance.COSINE
            )
        )
    return qdrant


def save_preguntes_mongo_qdrant(documents, collection_name):
    mongodb = MongoDBClient()
    db = mongodb.getDatabase(Config.MONGO_DB)
    collection = mongodb.getCollection(collection_name)
    mongodb.clearCollection(collection_name)

    qdrant = init_qdrant_collection(collection_name=collection_name)
    embedding_function = OpenAIEmbeddings()

    points = []
    for doc in documents:
        uid = str(uuid.uuid4())
        pregunta = doc["pregunta"]
        resposta = doc["resposta"]

        collection.insert_one({
            "_id": uid,
            "pregunta": pregunta,
            "resposta": resposta
        })

        embedding = embedding_function.embed_query(pregunta)
        points.append(PointStruct(
            id=uid,
            vector=embedding,
            payload={"mongo_id": uid}
        ))

    qdrant.upsert(collection_name=collection_name, points=points)
    print(f"✅ {len(points)} preguntes indexades a Qdrant i guardades a Mongo")

async def chunkize_async():
    await asyncio.to_thread(chunkize)


if __name__ == "__main__":
    print(Config.DATA_PATH)
    chunkize()
