from flask import Flask, request, jsonify
import os
import datetime
import mysql.connector
from whoosh.index import create_in, open_dir, exists_in
from whoosh.fields import Schema, TEXT, ID
from whoosh.qparser import MultifieldParser
import shutil

app = Flask(__name__)

# Assuming you've added an 'id' column in your MySQL database
schema = Schema(id=ID(stored=True), url=ID(stored=True), content=TEXT(stored=True))

indexdir = "indexdir"

def check_indexing_needed():
    if os.path.exists("last-indexed.txt"):
        with open("last-indexed.txt", "r") as f:
            last_indexed = f.read().strip()
            today = datetime.datetime.now().date().isoformat()
            return last_indexed != today
    return True

def update_last_indexed_date():
    with open("last-indexed.txt", "w") as f:
        f.write(datetime.datetime.now().date().isoformat())

def fetch_articles():
    connection = mysql.connector.connect(host='localhost', database='hubspot', user='zed', password='AchZun^28@@xx__144')
    cursor = connection.cursor()
    cursor.execute("SELECT id, url, content FROM articles")  # Assuming 'id' is your primary key
    articles = cursor.fetchall()
    cursor.close()
    connection.close()
    return articles

def recreate_index_dir():
    if os.path.exists(indexdir):
        shutil.rmtree(indexdir)
    os.makedirs(indexdir)

def index_articles(articles):
    recreate_index_dir()
    ix = create_in(indexdir, schema)
    writer = ix.writer()
    for article in articles:
        writer.add_document(id=str(article[0]), url=article[1], content=article[2])  # Ensure 'id' is a string
    writer.commit()

def search_articles(search_query):
    ix = open_dir(indexdir)
    parser = MultifieldParser(["id", "url", "content"], schema=ix.schema)
    query = parser.parse(search_query)
    results_list = []
    with ix.searcher() as searcher:
        results = searcher.search(query, limit=20)
        for result in results:
            results_list.append({"id": result['id'], "text": result['content'], "url": result['url']})
    return results_list

@app.route('/', methods=['GET', 'POST'])
def search():
    if request.method == 'POST':
        # For POST, assuming JSON body
        data = request.json
        search_query = data.get('query', '')
    else:
        # For GET, query parameters
        search_query = request.args.get('query', '')

    print(f"Received search query: {search_query}")  # Printing the received data

    if search_query:
        if check_indexing_needed():
            articles = fetch_articles()
            index_articles(articles)
            update_last_indexed_date()
        results = search_articles(search_query)
        return jsonify({"results": results})
    else:
        # Return predefined JSON if no query is provided
        return jsonify({
            "results": [
                {
                    "id": "99999999999",
                    "text": "This is a test",
                    "url": "https://google.com"
                }
            ]
        })

if __name__ == "__main__":
    app.run(debug=True, port=5000, host='0.0.0.0')