# -*- coding: utf-8 -*-
"""开放平台端点：开发者工作台 / 应用 / 插件 / 技能 / API 凭证。"""
from __future__ import annotations

from fastapi import APIRouter, Depends

from ..dependencies import get_db
from ...rbac import require_roles
from ...infrastructure.repositories import Database

router = APIRouter(prefix="/developer", tags=["developer"])


async def _page(port: str, page: str):
    async def handler(db: Database = Depends(get_db)):
        return await db.portal_pages.get(port, page) or {"items": []}

    return handler


@router.get("/dashboard", summary="开发者工作台")
async def dev_dashboard(db: Database = Depends(get_db), _u: dict = Depends(require_roles("developer"))):
    return await db.portal_pages.get("developer", "dashboard") or {"stats": []}


@router.get("/apps", summary="我的应用")
async def dev_apps(db: Database = Depends(get_db), _u: dict = Depends(require_roles("developer"))):
    return await db.portal_pages.get("developer", "apps") or {"items": []}


@router.get("/plugins", summary="我的插件")
async def dev_plugins(db: Database = Depends(get_db), _u: dict = Depends(require_roles("developer"))):
    return await db.portal_pages.get("developer", "plugins") or {"items": []}


@router.get("/skills", summary="技能市场")
async def dev_skills(db: Database = Depends(get_db), _u: dict = Depends(require_roles("developer"))):
    return await db.portal_pages.get("developer", "skills") or {"items": []}


@router.get("/api-keys", summary="API 凭证")
async def dev_api_keys(db: Database = Depends(get_db), _u: dict = Depends(require_roles("developer"))):
    return {"items": [
        {"id": "key_001", "name": "生产环境", "prefix": "pa_live_****abcd", "created_at": "2026-07-01"},
        {"id": "key_002", "name": "测试环境", "prefix": "pa_test_****wxyz", "created_at": "2026-07-05"},
    ], "note": "演示：凭证仅展示脱敏前缀"}
