-- ============================================================================
-- Rule Domain Tenant Phase 1 Verify
-- 目标：
--   1. 验证字段与索引已成功补齐
--   2. 验证历史资产已具备基础 tenant_code / scope_type
--   3. 验证运行结果链路具备租户快照
-- ============================================================================

-- 1. 字段检查
SELECT
    table_name,
    column_name
FROM information_schema.columns
WHERE table_schema = current_schema()
  AND (
    (table_name = 'leaudit_rule_sets' AND column_name IN ('tenant_code', 'scope_type', 'source_rule_set_id', 'tenant_name_snapshot'))
    OR (table_name = 'leaudit_rule_versions' AND column_name IN ('tenant_code_snapshot', 'scope_type_snapshot', 'source_version_id'))
    OR (table_name = 'leaudit_rule_group_bindings' AND column_name IN ('tenant_code', 'scope_type', 'tenant_name_snapshot'))
    OR (table_name = 'leaudit_rule_type_bindings' AND column_name IN ('tenant_code', 'scope_type'))
    OR (table_name = 'leaudit_audit_runs' AND column_name IN ('tenant_code', 'tenant_name_snapshot', 'scope_type_snapshot', 'group_id_snapshot', 'rule_binding_id_snapshot'))
    OR (table_name = 'leaudit_rule_results' AND column_name IN ('tenant_code', 'tenant_name_snapshot'))
    OR (table_name = 'leaudit_run_errors' AND column_name IN ('tenant_code', 'tenant_name_snapshot'))
    OR (table_name = 'leaudit_run_metrics' AND column_name IN ('tenant_code'))
  )
ORDER BY table_name, column_name;

-- 2. 历史规则资产 tenant_code / scope_type 覆盖率
SELECT
    COUNT(*) FILTER (WHERE tenant_code IS NULL OR BTRIM(tenant_code) = '') AS rule_sets_missing_tenant_code,
    COUNT(*) FILTER (WHERE scope_type IS NULL OR BTRIM(scope_type) = '') AS rule_sets_missing_scope_type
FROM leaudit_rule_sets
WHERE deleted_at IS NULL;

SELECT
    COUNT(*) FILTER (WHERE tenant_code IS NULL OR BTRIM(tenant_code) = '') AS rule_group_bindings_missing_tenant_code,
    COUNT(*) FILTER (WHERE scope_type IS NULL OR BTRIM(scope_type) = '') AS rule_group_bindings_missing_scope_type
FROM leaudit_rule_group_bindings
WHERE deleted_at IS NULL;

-- 3. 运行与结果快照覆盖率
SELECT
    COUNT(*) FILTER (WHERE tenant_code IS NULL OR BTRIM(tenant_code) = '') AS audit_runs_missing_tenant_code,
    COUNT(*) FILTER (WHERE scope_type_snapshot IS NULL OR BTRIM(scope_type_snapshot) = '') AS audit_runs_missing_scope_type
FROM leaudit_audit_runs;

SELECT
    COUNT(*) FILTER (WHERE tenant_code IS NULL OR BTRIM(tenant_code) = '') AS rule_results_missing_tenant_code
FROM leaudit_rule_results;

SELECT
    COUNT(*) FILTER (WHERE tenant_code IS NULL OR BTRIM(tenant_code) = '') AS run_errors_missing_tenant_code
FROM leaudit_run_errors;

SELECT
    COUNT(*) FILTER (WHERE tenant_code IS NULL OR BTRIM(tenant_code) = '') AS run_metrics_missing_tenant_code
FROM leaudit_run_metrics;

-- 4. 作用域分布，确认默认回填结果
SELECT scope_type, COUNT(*) AS total
FROM leaudit_rule_sets
WHERE deleted_at IS NULL
GROUP BY scope_type
ORDER BY scope_type;

SELECT scope_type, COUNT(*) AS total
FROM leaudit_rule_group_bindings
WHERE deleted_at IS NULL
GROUP BY scope_type
ORDER BY scope_type;
