import os
from flask import Blueprint, redirect, url_for, render_template, g, session,flash
from typing import cast

from app.utils.session_user import SessionUser
from app.services.appwrite_client import AppWriteClient
from app.services.appwrite_db import AppwriteTables
from app.services.coc_api import CoCApi

main_bp = Blueprint("main", __name__, url_prefix="/main")
cocapi = CoCApi()


def get_current_user() -> SessionUser:
    return cast(SessionUser, g.current_user)


@main_bp.route("/dashboard")
def dashboard():
    db_tables = AppwriteTables()
    user = get_current_user()
    results = db_tables.get_characters_for_user_id(user.id)
    if len(results) == 0:
        return redirect(url_for("char.create_char"))
    else:
        char=results
    return render_template("main/dashboard.html", profile=g.current_user, jwt_info=char)
