import json
from pathlib import Path

base = Path(__file__).resolve().parents[1] / 'raw'
source = json.loads((base / 'apifox_before_staff_uuid_20261012.json').read_text())
paths = ['/p/api/userdata', '/p/api/facedataLearnResult']
spec = {
    'openapi': '3.0.0',
    'info': {'title': '智慧营养健康餐厅设备人脸接口增量更新', 'version': '1.0.0'},
    'paths': {path: {'post': source['paths'][path]['post']} for path in paths},
}
for path in paths:
    operation = spec['paths'][path]['post']
    # targetEndpointFolderId already selects 智能硬件/安卓; absolute folder tags
    # would create a duplicate 智能硬件/安卓/智能硬件/安卓 hierarchy.
    operation.pop('tags', None)
    operation.pop('x-apifox-folder', None)

userdata = spec['paths']['/p/api/userdata']['post']
userdata['description'] = userdata['description'].replace(
    '学习结果回传时传作 `staff_id`',
    '学习结果回传时使用同一记录的 `staff_uuid`，`id` 不作为回传参数',
)
userdata['description'] = userdata['description'].replace(
    '学习成功后调用 `/p/api/facedataLearnResult` 回传。',
    '学习成功后调用 `/p/api/facedataLearnResult` 回传，`staff_uuid` 取自当前 `face_data[]` 记录。',
)

callback = spec['paths']['/p/api/facedataLearnResult']['post']
callback['description'] = callback['description'].replace(
    '| `staff_id` | Integer | 是 | `/p/api/facedata` 的 `face_data[].id`，须大于 0 | `123` |',
    '| `staff_uuid` | String | 是 | `/p/api/userdata` 的 `data.face_data[].staff_uuid`；最长 64 字节 | `11111111-2222-3333-4444-555555555555` |',
).replace('原样提交全量接口返回的完整照片 URL', '原样提交 `/p/api/userdata` 返回的完整照片 URL')
callback['description'] = callback['description'].replace(
    '人员 ID 和调用时间', '人员 UUID 和调用时间',
)
callback['description'] = callback['description'].replace(
    '{"code":0,"message":"人脸学习结果已保存","data":null,"trace_id":"<本次请求追踪ID>"}',
    '{\n  "code": 0,\n  "message": "人脸学习结果已保存",\n  "data": null,\n  "trace_id": "<本次请求追踪ID>"\n}',
).replace(
    '{"code":1,"message":"人脸照片已更新，请重新拉取","data":null,"trace_id":"<本次请求追踪ID>"}',
    '{\n  "code": 1,\n  "message": "人脸照片已更新，请重新拉取",\n  "data": null,\n  "trace_id": "<本次请求追踪ID>"\n}',
).replace(
    '{"code":1,"message":"设备不存在","data":null,"trace_id":"<本次请求追踪ID>"}',
    '{\n  "code": 1,\n  "message": "设备不存在",\n  "data": null,\n  "trace_id": "<本次请求追踪ID>"\n}',
)
body = callback['requestBody']['content']['application/x-www-form-urlencoded']
schema = body['schema']
schema['properties']['staff_uuid'] = {
    'type': 'string', 'minLength': 1, 'maxLength': 64,
    'description': '人员 UUID，来自 /p/api/userdata 的 data.face_data[].staff_uuid。',
    'example': '11111111-2222-3333-4444-555555555555',
}
schema['properties'].pop('staff_id', None)
schema['required'] = ['staff_uuid' if value == 'staff_id' else value for value in schema['required']]
body['example']['staff_uuid'] = '11111111-2222-3333-4444-555555555555'
body['example'].pop('staff_id', None)
schema['properties']['head_img_url']['description'] = '原样使用 /p/api/userdata 下发的完整 URL。'

output = base / 'apifox_staff_uuid_incremental_20261012.json'
output.write_text(json.dumps(spec, ensure_ascii=False, indent=2))
print(output)
