-- campaign_status 'failed' + RLS on core app tables (001). Enrichment RLS: see 004.

ALTER TYPE campaign_status ADD VALUE IF NOT EXISTS 'failed';

ALTER TABLE contacts ENABLE ROW LEVEL SECURITY;
ALTER TABLE lists ENABLE ROW LEVEL SECURITY;
ALTER TABLE list_contacts ENABLE ROW LEVEL SECURITY;
ALTER TABLE templates ENABLE ROW LEVEL SECURITY;
ALTER TABLE campaigns ENABLE ROW LEVEL SECURITY;
ALTER TABLE campaign_recipients ENABLE ROW LEVEL SECURITY;
ALTER TABLE email_events ENABLE ROW LEVEL SECURITY;
ALTER TABLE tracked_links ENABLE ROW LEVEL SECURITY;
ALTER TABLE tracked_link_clicks ENABLE ROW LEVEL SECURITY;
ALTER TABLE transactional_emails ENABLE ROW LEVEL SECURITY;
ALTER TABLE app_settings ENABLE ROW LEVEL SECURITY;

DROP POLICY IF EXISTS contacts_auth_all ON contacts;
DROP POLICY IF EXISTS lists_auth_all ON lists;
DROP POLICY IF EXISTS list_contacts_auth_all ON list_contacts;
DROP POLICY IF EXISTS templates_auth_all ON templates;
DROP POLICY IF EXISTS campaigns_auth_all ON campaigns;
DROP POLICY IF EXISTS campaign_recipients_auth_all ON campaign_recipients;
DROP POLICY IF EXISTS email_events_auth_all ON email_events;
DROP POLICY IF EXISTS tracked_links_auth_all ON tracked_links;
DROP POLICY IF EXISTS tracked_link_clicks_auth_all ON tracked_link_clicks;
DROP POLICY IF EXISTS transactional_emails_auth_all ON transactional_emails;
DROP POLICY IF EXISTS app_settings_auth_all ON app_settings;

CREATE POLICY contacts_auth_all ON contacts
  FOR ALL TO authenticated USING (true) WITH CHECK (true);
CREATE POLICY lists_auth_all ON lists
  FOR ALL TO authenticated USING (true) WITH CHECK (true);
CREATE POLICY list_contacts_auth_all ON list_contacts
  FOR ALL TO authenticated USING (true) WITH CHECK (true);
CREATE POLICY templates_auth_all ON templates
  FOR ALL TO authenticated USING (true) WITH CHECK (true);
CREATE POLICY campaigns_auth_all ON campaigns
  FOR ALL TO authenticated USING (true) WITH CHECK (true);
CREATE POLICY campaign_recipients_auth_all ON campaign_recipients
  FOR ALL TO authenticated USING (true) WITH CHECK (true);
CREATE POLICY email_events_auth_all ON email_events
  FOR ALL TO authenticated USING (true) WITH CHECK (true);
CREATE POLICY tracked_links_auth_all ON tracked_links
  FOR ALL TO authenticated USING (true) WITH CHECK (true);
CREATE POLICY tracked_link_clicks_auth_all ON tracked_link_clicks
  FOR ALL TO authenticated USING (true) WITH CHECK (true);
CREATE POLICY transactional_emails_auth_all ON transactional_emails
  FOR ALL TO authenticated USING (true) WITH CHECK (true);
CREATE POLICY app_settings_auth_all ON app_settings
  FOR ALL TO authenticated USING (true) WITH CHECK (true);
