-- 迁移：为 BOARD 看板新增 3 个 app schema RLS 视图
-- 日期：2026-03-19
-- 原因：BOARD-1 技能筛选、BOARD-2 项目筛选/消费潜力指数需要这些视图通过 FDW 暴露给业务库
-- 回滚：DROP VIEW IF EXISTS app.v_dws_assistant_project_tag, app.v_dws_member_project_tag, app.v_dws_member_spending_power_index;

-- 1. 助教项目标签视图（BOARD-1 技能筛选用）
CREATE OR REPLACE VIEW app.v_dws_assistant_project_tag AS
SELECT id, site_id, tenant_id, assistant_id, time_window,
       category_code, category_name, short_name,
       duration_seconds, total_seconds, percentage, is_tagged,
       computed_at, created_at, updated_at
FROM dws.dws_assistant_project_tag
WHERE site_id = current_setting('app.current_site_id')::bigint;

-- 2. 会员项目标签视图（BOARD-2 项目筛选用）
CREATE OR REPLACE VIEW app.v_dws_member_project_tag AS
SELECT id, site_id, tenant_id, member_id, time_window,
       category_code, category_name, short_name,
       duration_seconds, total_seconds, percentage, is_tagged,
       computed_at, created_at, updated_at
FROM dws.dws_member_project_tag
WHERE site_id = current_setting('app.current_site_id')::bigint;

-- 3. 会员消费潜力指数视图（BOARD-2 potential 维度用）
CREATE OR REPLACE VIEW app.v_dws_member_spending_power_index AS
SELECT spi_id, site_id, member_id,
       spend_30, spend_90, recharge_90,
       orders_30, orders_90, visit_days_30, visit_days_90,
       avg_ticket_90, active_weeks_90, daily_spend_ewma_90,
       score_level_raw, score_speed_raw, score_stability_raw,
       score_level_display, score_speed_display, score_stability_display,
       raw_score, display_score,
       calc_time, created_at, updated_at
FROM dws.dws_member_spending_power_index
WHERE site_id = current_setting('app.current_site_id')::bigint;
