-- 112_ops_print_error_feature_matrix.sql
-- Tambahkan feature ops.print_error dan mapping plan package.

INSERT INTO platform_features (code, name, domain, description, is_active, sort_order)
SELECT
  'ops.print_error',
  'Print Error & Loss Control',
  'operations',
  'Monitoring kesalahan operasional, analisis loss, dan kontrol pembebanan biaya ke karyawan secara opsional melalui payroll.',
  1,
  340
WHERE NOT EXISTS (
  SELECT 1
  FROM platform_features
  WHERE code = 'ops.print_error'
);

UPDATE platform_features
SET
  name = 'Print Error & Loss Control',
  domain = 'operations',
  description = 'Monitoring kesalahan operasional, analisis loss, dan kontrol pembebanan biaya ke karyawan secara opsional melalui payroll.',
  is_active = 1,
  sort_order = 340
WHERE code = 'ops.print_error';

INSERT INTO platform_plan_features (platform_plan_id, platform_feature_id, is_enabled)
SELECT pp.id, pf.id, 1
FROM platform_plans pp
INNER JOIN platform_features pf ON pf.code = 'ops.print_error'
WHERE pp.code IN ('operations', 'management', 'intelligence')
  AND NOT EXISTS (
    SELECT 1
    FROM platform_plan_features ppf
    WHERE ppf.platform_plan_id = pp.id
      AND ppf.platform_feature_id = pf.id
  );

UPDATE platform_plan_features ppf
INNER JOIN platform_plans pp ON pp.id = ppf.platform_plan_id
INNER JOIN platform_features pf ON pf.id = ppf.platform_feature_id
SET ppf.is_enabled = CASE
  WHEN pp.code IN ('operations', 'management', 'intelligence') THEN 1
  ELSE 0
END
WHERE pf.code = 'ops.print_error';
