# -*- coding: utf-8 -*-
"""
小程序备注路由 —— 备注 CRUD（含星星评分）。

端点清单：
- POST   /api/xcx/notes      — 创建备注
- GET    /api/xcx/notes       — 查询备注列表（query: target_type, target_id）
- DELETE /api/xcx/notes/{id}  — 删除备注

所有端点均需 JWT（approved 状态）。
"""

from __future__ import annotations

from fastapi import APIRouter, Depends, Query

from app.auth.dependencies import CurrentUser
from app.middleware.permission import require_approved
from app.schemas.xcx_notes import NoteCreateRequest, NoteOut
from app.services import note_service
from app.trace.decorators import trace_service

router = APIRouter(prefix="/api/xcx/notes", tags=["小程序备注"])


@router.post("", response_model=NoteOut)
@trace_service("创建备注", "Create note")
async def create_note(
    body: NoteCreateRequest,
    user: CurrentUser = Depends(require_approved()),
):
    """创建备注（含星星评分，可选关联任务）。"""
    return await note_service.create_note(
        site_id=user.site_id,
        user_id=user.user_id,
        target_type=body.target_type,
        target_id=body.target_id,
        content=body.content,
        task_id=body.task_id,
        rating_service_willingness=body.rating_service_willingness,
        rating_revisit_likelihood=body.rating_revisit_likelihood,
        score=body.score,
    )


@router.get("")
@trace_service("查询备注列表", "Get notes")
async def get_notes(
    target_type: str = Query("member", description="目标类型"),
    target_id: int = Query(..., description="目标 ID"),
    user: CurrentUser = Depends(require_approved()),
):
    """查询某目标的备注列表（按创建时间倒序）。"""
    return await note_service.get_notes(
        site_id=user.site_id,
        target_type=target_type,
        target_id=target_id,
    )


@router.delete("/{note_id}")
@trace_service("删除备注", "Delete note")
async def delete_note(
    note_id: int,
    user: CurrentUser = Depends(require_approved()),
):
    """删除备注（验证归属后硬删除）。"""
    return await note_service.delete_note(
        note_id=note_id,
        user_id=user.user_id,
        site_id=user.site_id,
    )
