import pandas as pd 
from pypdf import PdfReader 
import io 
import os 

def chunk_text (text ,target_length =2000 ,overlap =100 ):
    if not text :
        return []

    chunks =[]
    paragraphs =text .split ('\n\n')
    current_chunk =""

    for para in paragraphs :
        if len (current_chunk )+len (para )>target_length :
            if current_chunk :
                chunks .append (current_chunk .strip ())

            if len (para )>target_length :
                start =0 
                while start <len (para ):
                    end =start +target_length 
                    chunks .append (para [start :end ].strip ())
                    start +=(target_length -overlap )
                current_chunk =""
            else :
                current_chunk =para 
        else :
            if current_chunk :
                current_chunk +="\n\n"+para 
            else :
                current_chunk =para 

    if current_chunk :
        chunks .append (current_chunk .strip ())

    return chunks 

def load_csv (file_path ):
    df =pd .read_csv (file_path )
    return df .apply (lambda x :' | '.join (x .astype (str )),axis =1 ).tolist ()

def load_pdf (file_path ):
    reader =PdfReader (file_path )
    text_chunks =[]
    for page in reader .pages :
        text =page .extract_text ()
        if text :
            if len (text )>4000 :
                text_chunks .extend (chunk_text (text ))
            else :
                text_chunks .append (text )
    return text_chunks 

def load_txt (file_path ):
    with open (file_path ,'r',encoding ='utf-8')as f :
        content =f .read ()
    return chunk_text (content )

def load_xlsx (file_path ):
    all_rows =[]
    try :
        sheets =pd .read_excel (file_path ,sheet_name =None )
    except Exception as e :
        raise ValueError (f"Pandas read_excel failed: {e }")

    for sheet_name ,df in sheets .items ():
        if df .empty :
            continue 
        df =df .fillna ("")
        for row in df .values :
            row_items =[str (x )for x in row if str (x ).strip ()!=""]
            if row_items :
                row_str =f"Sheet: {str (sheet_name )} | "+" | ".join (row_items )
                if len (row_str )>8000 :
                    all_rows .extend (chunk_text (row_str ))
                else :
                    all_rows .append (row_str )
    return all_rows 

def process_file (file_path ):
    ext =os .path .splitext (file_path )[1 ].lower ()
    if ext =='.csv':
        return load_csv (file_path )
    elif ext =='.pdf':
        return load_pdf (file_path )
    elif ext =='.txt':
        return load_txt (file_path )
    elif ext =='.xlsx':
        return load_xlsx (file_path )
    else :
        raise ValueError (f"Unsupported file type: {ext }")
