#!/bin/bash
# 最終驗證腳本
# 驗證人臉識別系統的所有組件

set -e

echo "================================================"
echo "人臉識別系統最終驗證"
echo "================================================"

# 1. 檢查數據庫表
echo -e "\n1. 檢查數據庫表..."
psql postgres://accusys@localhost:5432/momentry -c "
SELECT table_name, 
       (SELECT COUNT(*) FROM information_schema.columns WHERE table_name = t.table_name) as columns,
       (SELECT COUNT(*) FROM pg_indexes WHERE tablename = t.table_name) as indexes
FROM information_schema.tables t
WHERE table_schema = 'public' 
  AND table_name LIKE 'face_%'
ORDER BY table_name;
"

# 2. 檢查數據庫函數
echo -e "\n2. 檢查數據庫函數..."
psql postgres://accusys@localhost:5432/momentry -c "
SELECT proname, 
       pg_get_function_arguments(p.oid) as arguments,
       pg_get_function_result(p.oid) as returns
FROM pg_proc p
WHERE proname LIKE '%face%'
ORDER BY proname;
"

# 3. 測試 Python 環境
echo -e "\n3. 測試 Python 環境..."
python3 -c "
import sys
print(f'Python版本: {sys.version}')

try:
    import insightface
    print('✅ insightface 已安裝')
except ImportError:
    print('❌ insightface 未安裝')
    sys.exit(1)

try:
    import onnxruntime as ort
    providers = ort.get_available_providers()
    print(f'✅ onnxruntime 已安裝')
    print(f'  可用提供者: {providers}')
    
    if 'CoreMLExecutionProvider' in providers:
        print('   ✅ CoreML (MPS) 支援可用')
    else:
        print('   ⚠️  CoreML (MPS) 不可用')
        
except ImportError:
    print('❌ onnxruntime 未安裝')
    sys.exit(1)

try:
    import psycopg2
    print('✅ psycopg2 已安裝')
except ImportError:
    print('❌ psycopg2 未安裝')
    sys.exit(1)
"

# 4. 測試 Rust 編譯
echo -e "\n4. 測試 Rust 編譯..."
cd /Users/accusys/momentry_core_0.1
if cargo check --lib; then
	echo "✅ Rust 庫編譯檢查通過"
else
	echo "❌ Rust 編譯檢查失敗"
	exit 1
fi

# 5. 測試 API 文件存在
echo -e "\n5. 檢查 API 文件..."
API_FILES=(
	"src/api/face_recognition.rs"
	"src/api/server.rs"
	"src/api/mod.rs"
	"src/core/processor/face_recognition.rs"
	"scripts/face_recognition_processor.py"
	"scripts/face_registration.py"
	"migrations/006_face_recognition_tables.sql"
)

all_files_exist=true
for file in "${API_FILES[@]}"; do
	if [ -f "$file" ]; then
		echo "✅ $file"
	else
		echo "❌ $file (缺失)"
		all_files_exist=false
	fi
done

if [ "$all_files_exist" = true ]; then
	echo "✅ 所有必要文件都存在"
else
	echo "❌ 有些文件缺失"
	exit 1
fi

# 6. 測試簡單的數據庫操作
echo -e "\n6. 測試數據庫操作..."
psql postgres://accusys@localhost:5432/momentry -c "
-- 測試插入
SELECT find_or_create_face_identity(
    'final_validation_001',
    'Final Validation Test',
    NULL,
    '{\"test\": true, \"validation\": \"success\"}'::jsonb,
    '{\"source\": \"final_validation\"}'::jsonb
) AS identity_id;

-- 驗證插入
SELECT id, face_id, name, attributes->>'test' as test_result 
FROM face_identities 
WHERE face_id = 'final_validation_001';

-- 清理
DELETE FROM face_identities WHERE face_id = 'final_validation_001';
"

# 7. 總結
echo -e "\n================================================"
echo "驗證完成"
echo "================================================"
echo ""
echo "🎉 人臉識別系統驗證通過！"
echo ""
echo "系統組件狀態:"
echo "  ✅ 數據庫表結構"
echo "  ✅ 數據庫函數"
echo "  ✅ Python 環境"
echo "  ✅ Rust 編譯"
echo "  ✅ API 文件"
echo "  ✅ 數據庫操作"
echo ""
echo "下一步操作:"
echo "1. 啟動服務器: cargo run -- server"
echo "2. 註冊人臉: curl -X POST http://localhost:3002/api/v1/face/register"
echo "3. 識別人臉: curl -X POST http://localhost:3002/api/v1/face/recognize"
echo "4. 搜索人臉: curl -X POST http://localhost:3002/api/v1/face/search"
echo ""
echo "MPS 加速已啟用，系統將自動使用 Apple Silicon 的 Metal Performance Shaders。"
echo "================================================"
