"""
授权相关 API 路由 — 单机版 v1.3.0
- GET /api/license/status  — 获取授权/试用状态
- POST /api/license/trial  — 激活试用码（30天，仅限一次）
- POST /api/license/activate — 激活正式序列号
"""
from fastapi import APIRouter, HTTPException
from pydantic import BaseModel, Field
from ..core.license import (
    get_machine_code,
    get_machine_code_unified,
    check_trial_status,
    activate_license,
    activate_trial,
)

router = APIRouter()


@router.get("/license/status")
async def license_status():
    """获取授权/试用状态"""
    status = await check_trial_status()
    status["machine_code"] = get_machine_code_unified()
    return status


@router.post("/license/trial")
async def license_trial():
    """激活试用码（30天，仅限一次）"""
    result = await activate_trial()
    if not result["success"]:
        raise HTTPException(status_code=400, detail=result["message"])
    return result


class ActivateRequest(BaseModel):
    license_key: str = Field(..., min_length=20, max_length=200, description="序列号（新格式已内嵌完整签名）")


@router.post("/license/activate")
async def license_activate(body: ActivateRequest):
    """激活正式序列号（新格式激活码已内嵌完整签名，无需单独传签名）"""
    result = await activate_license(body.license_key)
    if not result["success"]:
        raise HTTPException(status_code=400, detail=result["message"])
    return result
