from __future__ import annotations

from fastapi import APIRouter, Depends
from sqlalchemy.ext.asyncio import AsyncSession

from app.dependencies import get_admin_user, get_db
from app.models.user import User
from app.schemas.video_prompt_schema_config import (
    VideoPromptSchemaConfigImportRequest,
    VideoPromptSchemaConfigOut,
    VideoPromptSchemaConfigSaveRequest,
    VideoPromptSchemaExportOut,
    VideoPromptSchemaPreviewOut,
    VideoPromptSchemaPreviewRequest,
)
from app.services.video_prompt_schema_config_service import (
    export_video_prompt_schema_config,
    get_video_prompt_schema_config,
    import_video_prompt_schema_config,
    preview_runtime_schema,
    reset_video_prompt_schema_config,
    save_video_prompt_schema_config,
)

router = APIRouter(prefix="/admin/video-prompt-schema-config", tags=["admin-video-prompt-schema-config"])


@router.get("", response_model=VideoPromptSchemaConfigOut, summary="获取视频提词 Schema 配置")
async def get_config(
    admin: User = Depends(get_admin_user),
    db: AsyncSession = Depends(get_db),
):
    _ = admin
    return await get_video_prompt_schema_config(db)


@router.put("", response_model=VideoPromptSchemaConfigOut, summary="保存视频提词 Schema 配置")
async def save_config(
    req: VideoPromptSchemaConfigSaveRequest,
    admin: User = Depends(get_admin_user),
    db: AsyncSession = Depends(get_db),
):
    _ = admin
    return await save_video_prompt_schema_config(db, data=req.data, is_enabled=req.is_enabled)


@router.post("/reset-default", response_model=VideoPromptSchemaConfigOut, summary="恢复默认视频提词 Schema 配置")
async def reset_default(
    admin: User = Depends(get_admin_user),
    db: AsyncSession = Depends(get_db),
):
    _ = admin
    return await reset_video_prompt_schema_config(db)


@router.get("/export", response_model=VideoPromptSchemaExportOut, summary="导出视频提词 Schema 配置")
async def export_config(
    admin: User = Depends(get_admin_user),
    db: AsyncSession = Depends(get_db),
):
    _ = admin
    return await export_video_prompt_schema_config(db)


@router.post("/import", response_model=VideoPromptSchemaConfigOut, summary="导入视频提词 Schema 配置")
async def import_config(
    req: VideoPromptSchemaConfigImportRequest,
    admin: User = Depends(get_admin_user),
    db: AsyncSession = Depends(get_db),
):
    _ = admin
    return await import_video_prompt_schema_config(db, data=req.data, is_enabled=req.is_enabled)


@router.post("/preview", response_model=VideoPromptSchemaPreviewOut, summary="预览视频提词运行时 Schema")
async def preview_config(
    req: VideoPromptSchemaPreviewRequest,
    admin: User = Depends(get_admin_user),
):
    _ = admin
    return preview_runtime_schema(data=req.data, is_enabled=req.is_enabled, video_config=req.to_video_config())
