-- =====================================================================================
-- 数据分析模块（正式RPC）验证脚本
-- 文件: 99_ml_analytics_verify.sql
-- 说明:
-- 1) 先执行 01/02/03/04/05/06/07 等 RPC 脚本
-- 2) 再执行本文件，检查各 RPC 是否有返回
-- =====================================================================================

-- =============================
-- Dashboard（仪表盘）
-- =============================

SELECT *
FROM public.rpc_analytics_realtime_kpis();

SELECT public.rpc_analytics_online_users() AS online_users;


-- =============================
-- 用户分析
-- =============================

SELECT *
FROM public.rpc_analytics_user_kpis(
  (CURRENT_DATE - INTERVAL '6 days')::date,
  CURRENT_DATE
);

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

SELECT *
FROM public.rpc_analytics_user_segments(
  (CURRENT_DATE - INTERVAL '29 days')::date,
  CURRENT_DATE
);

SELECT *
FROM public.rpc_analytics_traffic_sources(
  (CURRENT_DATE - INTERVAL '29 days')::date,
  CURRENT_DATE
);


-- =============================
-- 销售/仪表盘趋势
-- =============================

SELECT *
FROM public.rpc_analytics_sales_kpis(
  (CURRENT_DATE - INTERVAL '6 days')::date,
  CURRENT_DATE
);

SELECT *
FROM public.rpc_analytics_sales_trend(
  (CURRENT_DATE - INTERVAL '29 days')::date,
  CURRENT_DATE
)
ORDER BY date;

SELECT *
FROM public.rpc_analytics_top_products(
  (CURRENT_DATE - INTERVAL '29 days')::date,
  CURRENT_DATE,
  10
);

SELECT *
FROM public.rpc_analytics_top_merchants(
  (CURRENT_DATE - INTERVAL '29 days')::date,
  CURRENT_DATE,
  10
);


-- =============================
-- 市场趋势
-- =============================

SELECT *
FROM public.rpc_analytics_market_trend_daily(
  (CURRENT_DATE - INTERVAL '29 days')::timestamptz,
  CURRENT_DATE::timestamptz
)
ORDER BY day;

SELECT *
FROM public.rpc_analytics_category_sales(
  (CURRENT_DATE - INTERVAL '29 days')::date,
  CURRENT_DATE
)
LIMIT 20;

SELECT *
FROM public.rpc_analytics_seasonal_trend(
  (CURRENT_DATE - INTERVAL '180 days')::date,
  CURRENT_DATE
);

SELECT *
FROM public.rpc_analytics_price_trend(
  (CURRENT_DATE - INTERVAL '29 days')::timestamptz,
  CURRENT_DATE::timestamptz
)
ORDER BY day;

SELECT *
FROM public.rpc_analytics_competition_share(
  (CURRENT_DATE - INTERVAL '29 days')::date,
  CURRENT_DATE
)
LIMIT 20;


-- =============================
-- 商品洞察
-- =============================

SELECT *
FROM public.rpc_product_insights_overview(
  (CURRENT_DATE - INTERVAL '29 days')::date,
  CURRENT_DATE
);

SELECT *
FROM public.rpc_product_insights_stock();

SELECT *
FROM public.rpc_product_insights_reviews();


-- =============================
-- 优惠券分析
-- =============================

SELECT *
FROM public.rpc_analytics_coupon_overview(
  (CURRENT_DATE - INTERVAL '29 days')::date,
  CURRENT_DATE
);

SELECT *
FROM public.rpc_analytics_coupon_by_type(
  (CURRENT_DATE - INTERVAL '29 days')::date,
  CURRENT_DATE
);

SELECT *
FROM public.rpc_analytics_coupon_by_channel(
  (CURRENT_DATE - INTERVAL '29 days')::date,
  CURRENT_DATE
);

SELECT *
FROM public.rpc_analytics_coupon_trend(
  (CURRENT_DATE - INTERVAL '29 days')::date,
  CURRENT_DATE
)
ORDER BY day;

SELECT *
FROM public.rpc_analytics_coupon_conversion(
  (CURRENT_DATE - INTERVAL '29 days')::date,
  CURRENT_DATE
);


-- =============================
-- 配送分析
-- =============================

SELECT *
FROM public.rpc_delivery_efficiency_daily(
  (CURRENT_DATE - INTERVAL '29 days')::timestamptz,
  CURRENT_DATE::timestamptz
)
ORDER BY day;

SELECT *
FROM public.rpc_delivery_efficiency_top_drivers(
  (CURRENT_DATE - INTERVAL '29 days')::timestamptz,
  CURRENT_DATE::timestamptz,
  10
);


-- =============================
-- 自定义报表 & 数据详情
-- =============================

-- 注意：这部分验证需要以一个真实用户身份执行（例如在 Supabase SQL Editor 中）

-- 1. 创建一个测试报表 (sales dataset)
SELECT public.rpc_create_custom_report(
  'Test Sales Report',
  'A test report for sales data',
  '{"dataset": "sales", "dimensions": ["date", "category"], "metrics": ["gmv", "orders"]}'::jsonb
) AS test_report_id;

-- 2. 获取报表列表，验证是否创建成功
SELECT * FROM public.rpc_get_custom_reports();

-- 3. 获取报表定义
-- 提示：将下面的 UUID 替换为步骤1返回的 test_report_id
-- SELECT * FROM public.rpc_data_detail_report_info('your-test-report-id-here');

-- 4. 执行报表
-- 提示：将下面的 UUID 替换为步骤1返回的 test_report_id
-- SELECT * FROM public.rpc_data_detail_rows('your-test-report-id-here');

-- 5. 删除测试报表
-- 提示：将下面的 UUID 替换为步骤1返回的 test_report_id
-- SELECT public.rpc_delete_custom_report('your-test-report-id-here');
