from flask import Flask, render_template, request, redirect, url_for
import sqlite3

app = Flask(__name__)

def get_match_logs(db_name="conversation.db"):
    conn = sqlite3.connect(db_name)
    c = conn.cursor()
    c.execute("SELECT id, question, answer, audio_path FROM conversation")
    logs = c.fetchall()
    conn.close()
    return logs

def delete_log(log_id, db_name="conversation.db"):
    conn = sqlite3.connect(db_name)
    c = conn.cursor()
    c.execute("DELETE FROM conversation WHERE id = ?", (log_id,))
    conn.commit()
    conn.close()

@app.route('/')
def index():
    logs = get_match_logs()
    return render_template('index.html', logs=logs)

@app.route('/delete/<int:log_id>', methods=['POST'])
def delete(log_id):
    delete_log(log_id)
    return redirect(url_for('index'))

if __name__ == '__main__':
    app.run(debug=True)
