#!/bin/bash
# API测试脚本
BASE_URL="http://localhost:8081"
TOKEN="seatons3d"

echo "=========================================="
echo "材质管理API接口测试"
echo "=========================================="

echo ""
echo "1. GET /api/v1/materials - 材质列表"
echo "------------------------------------------"
curl -s -H "X-API-Token: $TOKEN" "$BASE_URL/api/v1/materials?page=1&page_size=3" | jq .

echo ""
echo "2. POST /api/v1/materials - 添加材质"
echo "------------------------------------------"
NEW_MATERIAL=$(curl -s -X POST -H "X-API-Token: $TOKEN" -H "Content-Type: application/json" \
  -d '{"name":"测试材质API","diffuse_r":100,"diffuse_g":150,"diffuse_b":200,"alpha":255,"metallic":0.5,"roughness":0.3}' \
  "$BASE_URL/api/v1/materials")
echo "$NEW_MATERIAL" | jq .
NEW_ID=$(echo "$NEW_MATERIAL" | jq -r '.data.id')
echo "新建材质ID: $NEW_ID"

echo ""
echo "3. GET /api/v1/materials/:id - 获取材质详情"
echo "------------------------------------------"
curl -s -H "X-API-Token: $TOKEN" "$BASE_URL/api/v1/materials/$NEW_ID" | jq .

echo ""
echo "4. PUT /api/v1/materials/:id - 编辑材质"
echo "------------------------------------------"
curl -s -X PUT -H "X-API-Token: $TOKEN" -H "Content-Type: application/json" \
  -d '{"name":"测试材质API-已更新","diffuse_r":200,"diffuse_g":100,"diffuse_b":50,"alpha":255,"metallic":0.8,"roughness":0.2}' \
  "$BASE_URL/api/v1/materials/$NEW_ID" | jq .

echo ""
echo "5. DELETE /api/v1/materials/:id - 删除材质"
echo "------------------------------------------"
curl -s -X DELETE -H "X-API-Token: $TOKEN" "$BASE_URL/api/v1/materials/$NEW_ID" | jq .

echo ""
echo "=========================================="
echo "绑定管理API接口测试"
echo "=========================================="

echo ""
echo "6. POST /api/v1/materials/:id/bindings - 绑定材质"
echo "------------------------------------------"
curl -s -X POST -H "X-API-Token: $TOKEN" -H "Content-Type: application/json" \
  -d '{"group_ids": ["test_group_001", "test_group_002", "test_group_003"]}' \
  "$BASE_URL/api/v1/materials/4/bindings" | jq .

echo ""
echo "7. GET /api/v1/materials/:id/groups - 获取材质关联的groups"
echo "------------------------------------------"
curl -s -H "X-API-Token: $TOKEN" "$BASE_URL/api/v1/materials/4/groups" | jq '{material_id: .data.material_id, group_count: (.data.group_ids | length), sample_groups: (.data.group_ids[:5])}'

echo ""
echo "8. POST /api/v1/groups/materials - 根据group_ids查询材质"
echo "------------------------------------------"
curl -s -X POST -H "X-API-Token: $TOKEN" -H "Content-Type: application/json" \
  -d '{"group_ids": ["test_group_001", "202510211057245681447"]}' \
  "$BASE_URL/api/v1/groups/materials" | jq .

echo ""
echo "9. DELETE /api/v1/materials/:id/bindings - 解绑材质"
echo "------------------------------------------"
curl -s -X DELETE -H "X-API-Token: $TOKEN" -H "Content-Type: application/json" \
  -d '{"group_ids": ["test_group_001", "test_group_002", "test_group_003"]}' \
  "$BASE_URL/api/v1/materials/4/bindings" | jq .

echo ""
echo "=========================================="
echo "错误处理测试"
echo "=========================================="

echo ""
echo "10. 无Token访问"
echo "------------------------------------------"
curl -s "$BASE_URL/api/v1/materials" | jq .

echo ""
echo "11. 访问不存在的材质"
echo "------------------------------------------"
curl -s -H "X-API-Token: $TOKEN" "$BASE_URL/api/v1/materials/99999999" | jq .

echo ""
echo "=========================================="
echo "测试完成！"
echo "=========================================="
