from typing import Annotated

from fastapi import Depends
from fastapi import FastAPI
from fastapi import Request
from fastapi import Response
from fastapi.responses import FileResponse
from fastapi.responses import HTMLResponse
from fastapi.responses import RedirectResponse
from fastapi.staticfiles import StaticFiles

from ghostforge.db import create_db_and_tables
from ghostforge.db import User
from ghostforge.ethnicities import gf as gf_ethnicities
from ghostforge.ghosts import gf as gf_ghosts
from ghostforge.pages import gf as gf_pages
from ghostforge.templates import templates
from ghostforge.users import fastapi_users
from ghostforge.users import get_current_user
from ghostforge.users import gf as gf_users
from ghostforge.users import jwt_backend
from ghostforge.users import UserCreate
from ghostforge.users import UserRead
from ghostforge.users import UserUpdate
from ghostforge.users import web_backend


gf = FastAPI()
gf.mount("/static", StaticFiles(directory="ghostforge/static"), name="static")
gf.include_router(gf_users)
gf.include_router(gf_ghosts)
gf.include_router(gf_pages)
gf.include_router(gf_ethnicities)

gf.include_router(fastapi_users.get_auth_router(jwt_backend), prefix="/auth/jwt", tags=["auth"])
gf.include_router(fastapi_users.get_auth_router(web_backend), prefix="/auth/cookie", tags=["auth"])
gf.include_router(
    fastapi_users.get_register_router(UserRead, UserCreate),
    prefix="/auth",
    tags=["auth"],
)
gf.include_router(
    fastapi_users.get_reset_password_router(),
    prefix="/auth",
    tags=["auth"],
)
gf.include_router(
    fastapi_users.get_verify_router(UserRead),
    prefix="/auth",
    tags=["auth"],
)
gf.include_router(
    fastapi_users.get_users_router(UserRead, UserUpdate),
    prefix="/users",
    tags=["users"],
)


@gf.on_event("startup")
async def on_startup():
    await create_db_and_tables()


@gf.get("/faker.mjs")
async def get_faker():
    response = FileResponse("ghostforge/static/js/es6/faker-8.0.2.mjs", media_type="application/javascript")
    response.headers["Cache-Control"] = "public, max-age=31536000, immutable"
    return response


@gf.get("/", response_class=HTMLResponse)
async def home(request: Request, current_user=Depends(get_current_user())):
    if current_user:
        return RedirectResponse(url="/dashboard")
    return RedirectResponse(url="/login")


@gf.get("/login", response_class=HTMLResponse)
async def login_form(request: Request, current_user=Depends(get_current_user(optional=True))):
    if current_user:
        return RedirectResponse(url="/dashboard")
    return templates.TemplateResponse("login.html", {"request": request})


@gf.get("/logout")
async def logout(response: Response):
    response.delete_cookie("ghostforge")
    response.headers["Location"] = "/login"
    response.status_code = 302
    return response


@gf.get("/dashboard")
async def dashboard(request: Request, current_user: Annotated[User, Depends(get_current_user(optional=True))]):
    if not current_user:
        return RedirectResponse(url="/login")
    crumbs = [("dashboard", False)]
    return templates.TemplateResponse("dashboard.html", {"request": request, "crumbs": crumbs, "user": current_user})
