from fastapi import APIRouter

from app import schemas
from app.services.feishu_client import FeishuClient

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


@router.get("/status", response_model=schemas.FeishuStatus)
def feishu_status() -> dict:
    return FeishuClient().connection_status()


@router.get("/setup-guide", response_model=schemas.FeishuSetupGuide)
def setup_guide() -> schemas.FeishuSetupGuide:
    return schemas.FeishuSetupGuide(
        required_env=[
            "FEISHU_APP_ID",
            "FEISHU_APP_SECRET",
            "FEISHU_APP_TOKEN 或 FEISHU_WIKI_NODE_TOKEN",
            "FEISHU_TABLE_ID_SESSION",
            "FEISHU_TABLE_ID_RAW_QA",
            "FEISHU_TABLE_ID_STANDARD_QA",
            "MOCK_MODE=false",
        ],
        required_permissions=[
            "bitable:app:readonly",
            "bitable:app:readwrite",
            "docx:document:readonly",
            "drive:drive:readonly",
            "如果要读取文档链接里的内容，需要把飞书应用/机器人加入目标文档协作者。",
        ],
        tables=[
            schemas.FeishuTableTemplate(
                table_key="session",
                table_name="答疑场次",
                env_name="FEISHU_TABLE_ID_SESSION",
                required_fields=[
                    "场次编号",
                    "标题",
                    "日期",
                    "答疑老师",
                    "飞书资料链接",
                    "转写稿",
                    "处理状态",
                    "问答总数",
                    "待审核数",
                    "已审核数",
                    "已入库数",
                    "失败原因",
                ],
            ),
            schemas.FeishuTableTemplate(
                table_key="raw_qa",
                table_name="原始问答",
                env_name="FEISHU_TABLE_ID_RAW_QA",
                required_fields=[
                    "问答编号",
                    "场次编号",
                    "原始问题",
                    "问题整理版",
                    "原始回答",
                    "回答整理版",
                    "AI建议标准问题",
                    "AI建议标准回答",
                    "回答人",
                    "一级主题",
                    "问题标签",
                    "课程阶段",
                    "适用人群",
                    "情绪强度",
                    "风险等级",
                    "风险类型",
                    "风险说明",
                    "是否需脱敏",
                    "脱敏状态",
                    "审核状态",
                    "建议入库",
                    "来源时间戳",
                    "审核备注",
                ],
            ),
            schemas.FeishuTableTemplate(
                table_key="standard_qa",
                table_name="标准问答",
                env_name="FEISHU_TABLE_ID_STANDARD_QA",
                required_fields=[
                    "标准编号",
                    "来源原始问答ID",
                    "来源场次ID",
                    "标准问题",
                    "标准回答",
                    "相似问法",
                    "一级主题",
                    "问题标签",
                    "课程阶段",
                    "适用人群",
                    "回答边界",
                    "禁止表达",
                    "风险等级",
                    "审核状态",
                    "调用状态",
                    "禁用原因",
                ],
            ),
        ],
        notes=[
            "场次表中只有处理状态为空、unprocessed 或 未处理 的记录会被后台扫描处理。",
            "如果暂时不接文档 API，可以直接把完整文字稿粘到场次表的 转写稿 字段。",
            "敏感密钥不要写入 system_settings，请写入 backend/.env 或运行环境变量。",
        ],
    )
