import os
from langchain_community.document_loaders import PyPDFLoader
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_community.embeddings.sentence_transformer import SentenceTransformerEmbeddings
from langchain_community.vectorstores import Chroma
from django.conf import settings

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DB_DIR = os.path.join(BASE_DIR, 'chroma_db')
DOKUMEN_DIR = os.path.join(BASE_DIR, 'dokumen_rujukan')

def hadam_pdf(nama_fail):
    laluan_fail = os.path.join(DOKUMEN_DIR, nama_fail)
    
    print(f"\n--- MEMULAKAN PROSES PENGHADAMAN: {nama_fail} ---")
    
    loader = PyPDFLoader(laluan_fail)
    dokumen = loader.load()
    
    pemotong = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
    helaian = pemotong.split_documents(dokumen)
    
    if len(helaian) == 0:
        os.remove(laluan_fail) 
        raise ValueError("Tiada teks dijumpai dalam PDF ini. Pastikan ia bukan gambar yang di-scan.")
        
    print(f"   -> Berjaya dipotong kepada {len(helaian)} bahagian.")
    
    # 3. Tukar ke Vektor dan Simpan di ChromaDB (menggunakan Model Lokal SentenceTransformer)
    embeddings = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2")
    vector_db = Chroma(persist_directory=DB_DIR, embedding_function=embeddings)
    vector_db.add_documents(helaian)
    
    print("✅ BERJAYA! Otak AI anda kini lebih bijak (powered by OpenAI)")

if __name__ == "__main__":
    hadam_pdf("dasar_kesihatan.pdf")