#Kemoverse - a gacha-style bot for the Fediverse.
#Copyright © 2025 Waifu
#
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU Affero General Public License as
#published by the Free Software Foundation, either version 3 of the
#License, or (at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU Affero General Public License for more details.
#
#You should have received a copy of the GNU Affero General Public License
#along with this program.  If not, see https://www.gnu.org/licenses/.

import sqlite3
import sys
import os

# Add bot directory to path to import config
sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'bot'))
import config

from flask import Flask, render_template, abort
from werkzeug.exceptions import HTTPException

app = Flask(__name__)

def get_db_connection():
    conn = sqlite3.connect(config.DB_PATH)
    conn.row_factory = sqlite3.Row
    return conn

@app.errorhandler(HTTPException)
def handle_exception(error):
    return render_template("_error.html", error=error), error.code

@app.route("/i404")
def i404():
    return abort(404)

@app.route('/')
def index():
    conn = get_db_connection()
    users = conn.execute('SELECT id, username FROM users').fetchall()
    top_users = conn.execute('''
        SELECT users.id, users.username, COUNT(pulls.id) AS pull_count
        FROM users
        LEFT JOIN pulls ON users.id = pulls.user_id
        GROUP BY users.id
        ORDER BY pull_count DESC
        LIMIT 5
    ''').fetchall()

    conn.close()
    return render_template('index.html', users=users, top_users=top_users)

@app.route('/user/<int:user_id>')
def user_profile(user_id):
    conn = get_db_connection()
    conn.row_factory = sqlite3.Row
    cursor = conn.cursor()

    cursor.execute('SELECT * FROM users WHERE id = ?', (user_id,))
    user = cursor.fetchone()
    if user is None:
        abort(404)

    cursor.execute('''
        SELECT pulls.timestamp, characters.name as character_name, characters.rarity
        FROM pulls
        JOIN characters ON pulls.character_id = characters.id
        WHERE pulls.user_id = ?
        ORDER BY pulls.timestamp DESC
    ''', (user_id,))
    pulls = cursor.fetchall()

    conn.close()
    return render_template('user.html', user=user, pulls=pulls)

@app.route('/about')
def about():
    return render_template('about.html')

@app.route('/submit')
def submit_character():
    return render_template('submit.html')


if __name__ == '__main__':
    app.run(host=config.BIND_ADDRESS, port=config.WEB_PORT, debug=True)
