-- =====================================================================================
-- Admin 用户统计模块 - 完整验证脚本
-- 说明: 验证 RPC 可用性 + 模拟前端调用参数，确保返回数据结构与前端期望一致
-- =====================================================================================

-- 1) 检查 RPC 是否已部署（与 analytics 共享）
SELECT routine_name, routine_type
FROM information_schema.routines
WHERE routine_schema = 'public'
  AND routine_name IN (
    'rpc_analytics_user_kpis',
    'rpc_analytics_user_growth_trend',
    'rpc_analytics_user_segments',
    'rpc_analytics_traffic_sources'
  )
ORDER BY routine_name;

-- 2) 模拟前端调用：最近 30 天（与页面默认一致）
-- 2.1) KPI
SELECT * FROM public.rpc_analytics_user_kpis(
  CURRENT_DATE - INTERVAL '29 days',
  CURRENT_DATE
);

-- 2.2) 趋势（日维度）
SELECT * FROM public.rpc_analytics_user_growth_trend(
  CURRENT_DATE - INTERVAL '29 days',
  CURRENT_DATE
)
ORDER BY date;

-- 3) 模拟前端调用：最近 7 天（快速验证）
-- 3.1) KPI
SELECT * FROM public.rpc_analytics_user_kpis(
  CURRENT_DATE - INTERVAL '6 days',
  CURRENT_DATE
);

-- 3.2) 趋势
SELECT * FROM public.rpc_analytics_user_growth_trend(
  CURRENT_DATE - INTERVAL '6 days',
  CURRENT_DATE
)
ORDER BY date;

-- 4) 检查依赖表是否存在
SELECT table_name, table_type
FROM information_schema.tables
WHERE table_schema = 'public'
  AND table_name IN ('ak_users', 'ml_orders', 'ml_browse_history')
ORDER BY table_name;

-- 5) 检查关键字段是否存在（避免 RPC 运行时报错）
SELECT
  table_name,
  column_name,
  data_type,
  is_nullable
FROM information_schema.columns
WHERE table_schema = 'public'
  AND table_name = 'ak_users'
  AND column_name IN ('id', 'created_at', 'registration_source')
ORDER BY column_name;

SELECT
  table_name,
  column_name,
  data_type,
  is_nullable
FROM information_schema.columns
WHERE table_schema = 'public'
  AND table_name = 'ml_orders'
  AND column_name IN ('id', 'user_id', 'created_at', 'payment_status')
ORDER BY column_name;

SELECT
  table_name,
  column_name,
  data_type,
  is_nullable
FROM information_schema.columns
WHERE table_schema = 'public'
  AND table_name = 'ml_browse_history'
  AND column_name IN ('user_id', 'created_at')
ORDER BY column_name;

-- 6) 数据完整性检查（确保有数据可查）
SELECT
  (SELECT COUNT(*) FROM public.ak_users) AS ak_users_cnt,
  (SELECT COUNT(*) FROM public.ml_orders) AS ml_orders_cnt,
  (SELECT COUNT(*) FROM public.ml_browse_history) AS ml_browse_history_cnt;

-- 7) 快速验证脚本（返回是否可用）
DO $$
DECLARE
  kpis_ok BOOLEAN := FALSE;
  trend_ok BOOLEAN := FALSE;
BEGIN
  -- 验证 KPI RPC
  BEGIN
    PERFORM 1 FROM public.rpc_analytics_user_kpis(
      CURRENT_DATE - INTERVAL '6 days',
      CURRENT_DATE
    ) LIMIT 1;
    kpis_ok := TRUE;
  EXCEPTION WHEN OTHERS THEN
    kpis_ok := FALSE;
  END;

  -- 验证趋势 RPC
  BEGIN
    PERFORM 1 FROM public.rpc_analytics_user_growth_trend(
      CURRENT_DATE - INTERVAL '6 days',
      CURRENT_DATE
    ) LIMIT 1;
    trend_ok := TRUE;
  EXCEPTION WHEN OTHERS THEN
    trend_ok := FALSE;
  END;

  IF kpis_ok AND trend_ok THEN
    RAISE NOTICE '✅ Admin 用户统计 RPC 验证通过';
  ELSE
    RAISE NOTICE '❌ Admin 用户统计 RPC 验证失败: kpis_ok=%, trend_ok=%', kpis_ok, trend_ok;
  END IF;
END $$;