-- Align resident payment visibility with the GateWise Business plan and above.

DELETE FROM "SubscriptionPlanFeature" feature
USING "SubscriptionPlan" plan
WHERE feature."planId" = plan."id"
  AND plan."code" IN ('SECURE', 'LUXURY')
  AND feature."feature" = 'SERVICE_CHARGE_PAYMENTS';

INSERT INTO "SubscriptionPlanFeature" ("id", "planId", "feature", "createdAt", "updatedAt")
SELECT
  'plan-feature-' || lower(plan."code") || '-plan-comparison-view',
  plan."id",
  'PLAN_COMPARISON_VIEW',
  CURRENT_TIMESTAMP,
  CURRENT_TIMESTAMP
FROM "SubscriptionPlan" plan
WHERE plan."code" IN ('BUSINESS', 'ENTERPRISE')
ON CONFLICT ("planId", "feature") DO NOTHING;

UPDATE "SubscriptionPlan"
SET "metadata" = CASE
  WHEN "code" = 'SECURE' THEN "metadata" || '{"residentPayments":false}'::jsonb
  WHEN "code" = 'LUXURY' THEN "metadata" || '{"residentPayments":false,"recommended":true}'::jsonb
  WHEN "code" = 'BUSINESS' THEN "metadata" || '{"residentPayments":true}'::jsonb
  WHEN "code" = 'ENTERPRISE' THEN "metadata" || '{"residentPayments":true}'::jsonb
  ELSE "metadata"
END
WHERE "code" IN ('SECURE', 'LUXURY', 'BUSINESS', 'ENTERPRISE');
