from fastapi import Request,Depends, APIRouter, UploadFile,HTTPException,File
from app.db import Base,engine
from app.db.session import get_db
from app.db.crud import *
from app.db.schemas import *
from typing import List
from app.core.auth import get_current_active_user,get_current_user
from app.core.apiexception import APIException

platform_router = r = APIRouter()

@r.get(
    "/appsettings/{id}",
    response_model=App,
    response_model_exclude_none=True,
)
async def appsetting_details(
    request: Request,
    id: int,
    db=Depends(get_db),
):
    try:
        app = get_appsetting(db, id)
        return app
    except Exception as e:
        raise APIException('platform:appsettings',request.url._url,f"Error occurred while get app setting:",e)

@r.post("/appsettings", response_model=App, response_model_exclude_none=True)
async def appsetting_create(
    request: Request,
    app: AppBase,
    db=Depends(get_db),
):
    try:
        return create_appsetting(db, app)
    except Exception as e:
        raise APIException('platform:appsettings',request.url._url,f"Error occurred while get create app setting:",e)


@r.put(
    "/appsettings/{id}", response_model=App, response_model_exclude_none=True
)
async def appsetting_edit(
    request: Request,
    id: int,
    app: AppEdit,
    db=Depends(get_db),
):
    try:
        return edit_appsetting(db, id, app)
    except Exception as e:
        raise APIException('platform:appsettings',request.url._url,f"Error occurred while edit app setting:",e)


@r.delete(
    "/appsettings/{id}", response_model=App, response_model_exclude_none=True
)
async def appsettings_delete(
    request: Request,
    id: int,
    db=Depends(get_db),
):
    try:
        return delete_appsetting(db, id)
    except Exception as e:
        raise APIException('platform:appsettings',request.url._url,f"Error occurred while delete app setting:",e)


@r.get(
    "/kintone/{type}",
    response_model=t.List[Kintone],
    response_model_exclude_none=True,
)
async def kintone_data(
    request: Request,
    type: int,
    db=Depends(get_db),
):
    try:
        kintone = get_kintones(db, type)
        return kintone
    except Exception as e:
        raise APIException('platform:kintone',request.url._url,f"Error occurred while get kintone env:",e)

@r.get(
    "/actions",
    response_model=t.List[Action],
    response_model_exclude={"id"},
    response_model_exclude_none=True,
)
async def action_data(
    request: Request,
    db=Depends(get_db),
):
    try:
        actions = get_actions(db)
        return actions
    except Exception as e:
        raise APIException('platform:actions',request.url._url,f"Error occurred while get actions:",e)

@r.get(
    "/flow/{flowid}",
    response_model=Flow,
    response_model_exclude_none=True,
)
async def flow_details(
    request: Request,
    flowid: str,
    db=Depends(get_db),
):
    try:
        app = get_flow(db, flowid)
        return app
    except Exception as e:
        raise APIException('platform:flow',request.url._url,f"Error occurred while get flow by flowid:",e)


@r.get(
    "/flows/{appid}",
    response_model=List[Flow],
    response_model_exclude_none=True,
)
async def flow_list(
    request: Request,
    appid: str,
    user=Depends(get_current_user),
    db=Depends(get_db),
):
    try:
        domain = get_activedomain(db, user.id)
        print("domain=>",domain)
        flows = get_flows_by_app(db, domain.id, appid)
        return flows
    except Exception as e:
        raise APIException('platform:flow',request.url._url,f"Error occurred while get flow by appid:",e)
    

@r.post("/flow", response_model=Flow, response_model_exclude_none=True)
async def flow_create(
    request: Request,
    flow: FlowBase,
    user=Depends(get_current_user),
    db=Depends(get_db),
):
    try:
        domain = get_activedomain(db, user.id)
        return create_flow(db, domain.id, flow)
    except Exception as e:
        raise APIException('platform:flow',request.url._url,f"Error occurred while create flow:",e)


@r.put(
    "/flow/{flowid}", response_model=Flow, response_model_exclude_none=True
)
async def flow_edit(
    request: Request,
    flow: FlowBase,
    db=Depends(get_db),
):
    try:
        return edit_flow(db, flow)
    except Exception as e:
        raise APIException('platform:flow',request.url._url,f"Error occurred while edit flow:",e)


@r.delete(
    "/flow/{flowid}", response_model=Flow, response_model_exclude_none=True
)
async def flow_delete(
    request: Request,
    flowid: str,
    db=Depends(get_db),
):
    try:
        return delete_flow(db, flowid)
    except Exception as e:
        raise APIException('platform:flow',request.url._url,f"Error occurred while delete flow:",e)

@r.get(
    "/domains/{tenantid}",
    response_model=List[Domain],
    response_model_exclude_none=True,
)
async def domain_details(
    request: Request,
    tenantid:str,
    db=Depends(get_db),
):
    try:
        domains = get_domains(db,tenantid)
        return domains
    except Exception as e:
        raise APIException('platform:domains',request.url._url,f"Error occurred while get domains:",e)

@r.post("/domain", response_model=Domain, response_model_exclude_none=True)
async def domain_create(
    request: Request,
    domain: DomainBase,
    db=Depends(get_db),
):
    try:
        return create_domain(db, domain)
    except Exception as e:
        raise APIException('platform:domain',request.url._url,f"Error occurred while create domain:",e)


@r.put(
    "/domain", response_model=Domain, response_model_exclude_none=True
)
async def domain_edit(
    request: Request,
    domain: DomainBase,
    db=Depends(get_db),
):
    try:
        return edit_domain(db, domain)
    except Exception as e:
        raise APIException('platform:domain',request.url._url,f"Error occurred while edit domain:",e)


@r.delete(
    "/domain/{id}", response_model=Domain, response_model_exclude_none=True
)
async def domain_delete(
    request: Request,
    id: int,
    db=Depends(get_db),
):
    try:
        return delete_domain(db,id)
    except Exception as e:
        raise APIException('platform:domain',request.url._url,f"Error occurred while delete domain:",e)

@r.get(
    "/domain",
    response_model=List[Domain],
    response_model_exclude_none=True,
)
async def userdomain_details(
    request: Request,
    user=Depends(get_current_user),
    db=Depends(get_db),
):
    try:
        domains = get_domain(db, user.id)
        return domains
    except Exception as e:
        raise APIException('platform:domain',request.url._url,f"Error occurred while get user({user.id}) domain:",e)

@r.post(
    "/domain/{userid}",
    response_model_exclude_none=True,
)
async def create_userdomain(
    request: Request,
    userid: int,
    domainids:list,
    db=Depends(get_db),
):
    try:
        domain = add_userdomain(db, userid,domainids)
        return domain
    except Exception as e:
        raise APIException('platform:domain',request.url._url,f"Error occurred while add user({userid}) domain:",e)

@r.delete(
    "/domain/{domainid}/{userid}", response_model_exclude_none=True
)
async def userdomain_delete(
    request: Request,
    domainid:int,
    userid: int,
    db=Depends(get_db),
):
    try:
        return delete_userdomain(db, userid,domainid)
    except Exception as e:
        raise APIException('platform:delete',request.url._url,f"Error occurred while delete user({userid}) domain:",e)


@r.get(
    "/activedomain",
    response_model=Domain,
    response_model_exclude_none=True,
)
async def get_useractivedomain(
    request: Request,
    user=Depends(get_current_user),
    db=Depends(get_db),
):
    try:
        domain = get_activedomain(db, user.id)
        return domain
    except Exception as e:
        raise APIException('platform:activedomain',request.url._url,f"Error occurred while get user({user.id}) activedomain:",e)

@r.put(
    "/activedomain/{domainid}",
    response_model_exclude_none=True,
)
async def update_activeuserdomain(
    request: Request,
    domainid:int,
    user=Depends(get_current_user),
    db=Depends(get_db),
):
    try:
        domain = active_userdomain(db, user.id,domainid)
        return domain
    except Exception as e:
        raise APIException('platform:activedomain',request.url._url,f"Error occurred while update user({user.id}) activedomain:",e)

@r.get(
    "/events",
    response_model=t.List[Event],
    response_model_exclude={"id"},
    response_model_exclude_none=True,
)
async def event_data(
    request: Request,
    db=Depends(get_db),
):
    try:
        events = get_events(db)
        return events
    except Exception as e:
        raise APIException('platform:events',request.url._url,f"Error occurred while get events:",e)


@r.get(
    "/eventactions/{eventid}",
    response_model=t.List[Action],
    response_model_exclude={"id"},
    response_model_exclude_none=True,
)
async def eventactions_data(
    request: Request,
    eventid: str,
    db=Depends(get_db),
):
    try:
        actions = get_eventactions(db,eventid)
        return actions
    except Exception as e:
        raise APIException('platform:eventactions',request.url._url,f"Error occurred while get eventactions:",e)