# SPDX-FileCopyrightText: 2025 Weibo, Inc.
#
# SPDX-License-Identifier: Apache-2.0

from app.api.endpoints import (
    admin,
    api_keys,
    attachments_open,
    auth,
    deep_research,
    devices,
    dingtalk_docs,
    groups,
    health,
    knowledge,
    knowledge_open,
    mcp_providers,
    oidc,
    openapi_responses,
    pet,
    projects,
    prompt_optimization,
    quota,
    repository,
    share,
    skill_identity,
    skill_market,
    subtasks,
    tables,
    token_issuers,
    users,
    utils,
    web_scraper,
    wiki,
    wizard,
    work_queue,
)
from app.api.endpoints.dingtalk_wikispace import router as dingtalk_wikispace_router
from app.core.config import settings

# RAG module is heavy (llama_index, scipy, pandas, grpc) - skip in standalone mode
if not settings.STANDALONE_MODE:
    from app.api.endpoints import rag

from app.api.endpoints.adapter import (
    agents,
    attachments,
    bots,
    chat,
    dify,
    model_runtime,
    models,
    retrievers,
    shells,
    subscription_follows,
    subscription_market,
    subscriptions,
    task_knowledge_bases,
    task_members,
    tasks,
    teams,
    templates,
)
from app.api.endpoints.internal import attachments_router as internal_attachments_router
from app.api.endpoints.internal import bots_router as internal_bots_router
from app.api.endpoints.internal import (
    callback_router,
    chat_storage_router,
    conversion_callback_router,
    object_storage_router,
    rag_content_router,
    services_router,
    skills_router,
    subscriptions_router,
    tables_router,
    workspace_archives_router,
)

# RAG internal router is conditionally imported based on STANDALONE_MODE
if not settings.STANDALONE_MODE:
    from app.api.endpoints.internal import rag_router

from app.api.endpoints.kind import k_router
from app.api.router import api_router

# Health check endpoints (no prefix, directly under /api)
api_router.include_router(health.router, tags=["health"])
api_router.include_router(auth.router, prefix="/auth", tags=["auth"])
api_router.include_router(oidc.router, prefix="/auth/oidc", tags=["auth", "oidc"])
api_router.include_router(users.router, prefix="/users", tags=["users"])
api_router.include_router(pet.router, prefix="/users/me/pet", tags=["pet"])
api_router.include_router(admin.router, prefix="/admin", tags=["admin"])
api_router.include_router(groups.router, prefix="/groups", tags=["groups"])
api_router.include_router(projects.router, prefix="/projects", tags=["projects"])
api_router.include_router(api_keys.router, prefix="/api-keys", tags=["api-keys"])
api_router.include_router(devices.router, prefix="/devices", tags=["devices"])
api_router.include_router(bots.router, prefix="/bots", tags=["bots"])
api_router.include_router(models.router, prefix="/models", tags=["public-models"])
api_router.include_router(shells.router, prefix="/shells", tags=["shells"])
api_router.include_router(agents.router, prefix="/agents", tags=["public-shell"])
api_router.include_router(teams.router, prefix="/teams", tags=["teams"])
api_router.include_router(
    subscriptions.router, prefix="/subscriptions", tags=["subscriptions"]
)
# Subscription follow endpoints (follow, unfollow, invitations)
api_router.include_router(
    subscription_follows.router, prefix="/subscriptions", tags=["subscription-follows"]
)
api_router.include_router(
    subscription_follows.user_router, prefix="/users/me", tags=["user-subscriptions"]
)
api_router.include_router(
    subscription_follows.invitation_router,
    prefix="/subscription-invitations",
    tags=["subscription-invitations"],
)
# Subscription market endpoints (browse, rent, rentals)
api_router.include_router(
    subscription_market.router, prefix="/market", tags=["subscription-market"]
)
api_router.include_router(tasks.router, prefix="/tasks", tags=["tasks"])
api_router.include_router(subtasks.router, prefix="/subtasks", tags=["subtasks"])
api_router.include_router(task_members.router, prefix="/tasks", tags=["task-members"])
api_router.include_router(
    task_knowledge_bases.router, prefix="/tasks", tags=["task-knowledge-bases"]
)
api_router.include_router(chat.router, prefix="/chat", tags=["chat"])
api_router.include_router(
    attachments.router, prefix="/attachments", tags=["attachments"]
)
api_router.include_router(repository.router, prefix="/git", tags=["repository"])
api_router.include_router(quota.router, prefix="/quota", tags=["quota"])
api_router.include_router(dify.router, prefix="/dify", tags=["dify"])
api_router.include_router(
    model_runtime.router, prefix="/model-runtime", tags=["model-runtime"]
)
api_router.include_router(retrievers.router, prefix="/retrievers", tags=["retrievers"])
api_router.include_router(wiki.router, prefix="/wiki", tags=["wiki"])
api_router.include_router(
    wiki.internal_router, prefix="/internal/wiki", tags=["wiki-internal"]
)
api_router.include_router(wizard.router, prefix="/wizard", tags=["wizard"])
api_router.include_router(
    openapi_responses.router, prefix="/v1/responses", tags=["openapi-responses"]
)
api_router.include_router(deep_research.router, prefix="/v1", tags=["deep-research"])
api_router.include_router(token_issuers.router, prefix="/v1", tags=["token-issuers"])
api_router.include_router(
    knowledge.router, prefix="/knowledge-bases", tags=["knowledge"]
)
api_router.include_router(
    knowledge.document_router, prefix="/knowledge-documents", tags=["knowledge"]
)
api_router.include_router(
    knowledge.qa_history_router,
    prefix="/v1/knowledge-base/qa-history",
    tags=["knowledge-qa-history"],
)
api_router.include_router(
    knowledge.summary_router,
    prefix="/knowledge-bases",
    tags=["knowledge-summary"],
)
api_router.include_router(
    attachments_open.router,
    prefix="/v1/attachments",
    tags=["attachments-open"],
)
api_router.include_router(
    knowledge_open.router,
    prefix="/knowledge",
    tags=["knowledge-open"],
)
# Unified share endpoints (Team, Task, KnowledgeBase)
api_router.include_router(share.router, prefix="/share", tags=["share"])
api_router.include_router(tables.router, prefix="/tables", tags=["tables"])
api_router.include_router(
    dingtalk_docs.router, prefix="/dingtalk-docs", tags=["dingtalk-docs"]
)
api_router.include_router(
    dingtalk_wikispace_router,
    prefix="/dingtalk-wikispace",
    tags=["dingtalk-wikispace"],
)

# Work queue endpoints (message forwarding and inbox)
api_router.include_router(
    work_queue.router, prefix="/work-queues", tags=["work-queues"]
)
# Template endpoints (browse and instantiate)
api_router.include_router(templates.router, prefix="/templates", tags=["templates"])
api_router.include_router(
    work_queue.messages_router, prefix="/queue-messages", tags=["queue-messages"]
)
api_router.include_router(
    work_queue.forward_router, prefix="/messages/forward", tags=["message-forwarding"]
)
api_router.include_router(
    work_queue.users_router, prefix="/users", tags=["user-queues"]
)

# RAG router is conditionally registered based on STANDALONE_MODE
if not settings.STANDALONE_MODE:
    api_router.include_router(rag.router, prefix="/rag", tags=["rag"])
api_router.include_router(
    mcp_providers.router, prefix="/mcp-providers", tags=["mcp-providers"]
)

api_router.include_router(utils.router, prefix="/utils", tags=["utils"])
api_router.include_router(
    web_scraper.router, prefix="/web-scraper", tags=["web-scraper"]
)
# Skill market endpoints (external skill market integration)
api_router.include_router(
    skill_market.router, prefix="/skill-market", tags=["skill-market"]
)
api_router.include_router(skill_identity.router, tags=["skill-identity"])
api_router.include_router(
    prompt_optimization.router,
    prefix="/prompt-optimization",
    tags=["prompt-optimization"],
)
api_router.include_router(k_router)

# Internal API endpoints (for service-to-service communication)
api_router.include_router(
    chat_storage_router, prefix="/internal", tags=["internal-chat"]
)
api_router.include_router(
    rag_content_router, prefix="/internal", tags=["internal-rag-content"]
)

# RAG internal router is conditionally registered based on STANDALONE_MODE
if not settings.STANDALONE_MODE:
    api_router.include_router(rag_router, prefix="/internal", tags=["internal-rag"])

api_router.include_router(skills_router, prefix="/internal", tags=["internal-skills"])
api_router.include_router(tables_router, prefix="/internal", tags=["internal-tables"])
api_router.include_router(
    internal_bots_router, prefix="/internal", tags=["internal-bots"]
)
api_router.include_router(
    services_router, prefix="/internal", tags=["internal-services"]
)
api_router.include_router(
    object_storage_router,
    prefix="/internal",
    tags=["internal-object-storage"],
)
api_router.include_router(
    workspace_archives_router,
    prefix="/internal",
    tags=["internal-workspace-archives"],
)
api_router.include_router(
    subscriptions_router, prefix="/internal", tags=["internal-subscriptions"]
)
api_router.include_router(
    callback_router, prefix="/internal", tags=["internal-callback"]
)
api_router.include_router(
    internal_attachments_router,
    prefix="/internal",
    tags=["internal-attachments"],
)
api_router.include_router(
    conversion_callback_router,
    prefix="/internal",
    tags=["internal-conversion-callback"],
)
