from fastapi import APIRouter, Depends, Query, status
from fastapi.responses import JSONResponse
from models.user import User
from pydantic import BaseModel
from auth.security import get_current_user
from config import settings
from utils.responses import success_response
from fudo.fudo import get_table



store_router = APIRouter()

class AppStateBody(BaseModel):
    state: bool

@store_router.post("/state", response_class=JSONResponse)
def set_store_state(state: AppStateBody, current_user: User = Depends(get_current_user)):
    if (current_user.permissions or -1) >= 1:
        settings.IS_OPEN_STORE = state.state
    
    return success_response({"state": settings.IS_OPEN_STORE})

@store_router.get("/tables/exists", response_class=JSONResponse)
def get_table_exists(q: str = Query(..., description="q parameter")):
    table = get_table(int(q))
    exist = bool(table)
    return success_response({"exists": exist}, status_code=200)

@store_router.get("/state", response_class=JSONResponse)
def get_store_state(_: User = Depends(get_current_user)):
    return success_response(data={"state": settings.IS_OPEN_STORE})