BEGIN;


ALTER TABLE mock.shop_amazon_events
	ADD COLUMN processed BOOLEAN DEFAULT FALSE,
	ADD COLUMN processed_successfully BOOLEAN DEFAULT FALSE;
CREATE INDEX shop_amazon_events_by_timestamp ON mock.shop_amazon_events (shop_id, event_timestamp);
CREATE INDEX shop_amazon_events_by_timestamp_unprocessed ON mock.shop_amazon_events (shop_id, event_timestamp) WHERE NOT processed;

ALTER TABLE mock.shop_big_cartel_events
	ADD COLUMN processed BOOLEAN DEFAULT FALSE,
	ADD COLUMN processed_successfully BOOLEAN DEFAULT FALSE;
CREATE INDEX shop_big_cartel_events_by_timestamp ON mock.shop_big_cartel_events (shop_id, event_timestamp);
CREATE INDEX shop_big_cartel_events_by_timestamp_unprocessed ON mock.shop_big_cartel_events (shop_id, event_timestamp) WHERE NOT processed;

ALTER TABLE mock.shop_ebay_events
	ADD COLUMN processed BOOLEAN DEFAULT FALSE,
	ADD COLUMN processed_successfully BOOLEAN DEFAULT FALSE;
CREATE INDEX shop_ebay_events_by_timestamp ON mock.shop_ebay_events (shop_id, event_timestamp);
CREATE INDEX shop_ebay_events_by_timestamp_unprocessed ON mock.shop_ebay_events (shop_id, event_timestamp) WHERE NOT processed;

ALTER TABLE mock.shop_ecwid_events
	ADD COLUMN processed BOOLEAN DEFAULT FALSE,
	ADD COLUMN processed_successfully BOOLEAN DEFAULT FALSE;
CREATE INDEX shop_ecwid_events_by_timestamp ON mock.shop_ecwid_events (shop_id, event_timestamp);
CREATE INDEX shop_ecwid_events_by_timestamp_unprocessed ON mock.shop_ecwid_events (shop_id, event_timestamp) WHERE NOT processed;

ALTER TABLE mock.shop_etsy_events
	ADD COLUMN processed BOOLEAN DEFAULT FALSE,
	ADD COLUMN processed_successfully BOOLEAN DEFAULT FALSE;
CREATE INDEX shop_etsy_events_by_timestamp ON mock.shop_etsy_events (shop_id, event_timestamp);
CREATE INDEX shop_etsy_events_by_timestamp_unprocessed ON mock.shop_etsy_events (shop_id, event_timestamp) WHERE NOT processed;

ALTER TABLE mock.shop_shopify_events
	ADD COLUMN processed BOOLEAN DEFAULT FALSE,
	ADD COLUMN processed_successfully BOOLEAN DEFAULT FALSE;
CREATE INDEX shop_shopify_events_by_timestamp ON mock.shop_shopify_events (shop_id, event_timestamp);
CREATE INDEX shop_shopify_events_by_timestamp_unprocessed ON mock.shop_shopify_events (shop_id, event_timestamp) WHERE NOT processed;

ALTER TABLE mock.shop_square_online_events
	ADD COLUMN processed BOOLEAN DEFAULT FALSE,
	ADD COLUMN processed_successfully BOOLEAN DEFAULT FALSE;
CREATE INDEX shop_square_online_events_by_timestamp ON mock.shop_square_online_events (shop_id, event_timestamp);
CREATE INDEX shop_square_online_events_by_timestamp_unprocessed ON mock.shop_square_online_events (shop_id, event_timestamp) WHERE NOT processed;

ALTER TABLE mock.shop_squarespace_events
	ADD COLUMN processed BOOLEAN DEFAULT FALSE,
	ADD COLUMN processed_successfully BOOLEAN DEFAULT FALSE;
CREATE INDEX shop_squarespace_events_by_timestamp ON mock.shop_squarespace_events (shop_id, event_timestamp);
CREATE INDEX shop_squarespace_events_by_timestamp_unprocessed ON mock.shop_squarespace_events (shop_id, event_timestamp) WHERE NOT processed;

ALTER TABLE mock.shop_tiktok_events
	ADD COLUMN processed BOOLEAN DEFAULT FALSE,
	ADD COLUMN processed_successfully BOOLEAN DEFAULT FALSE;
CREATE INDEX shop_tiktok_events_by_timestamp ON mock.shop_tiktok_events (shop_id, event_timestamp);
CREATE INDEX shop_tiktok_events_by_timestamp_unprocessed ON mock.shop_tiktok_events (shop_id, event_timestamp) WHERE NOT processed;

ALTER TABLE mock.shop_walmart_marketplace_events
	ADD COLUMN processed BOOLEAN DEFAULT FALSE,
	ADD COLUMN processed_successfully BOOLEAN DEFAULT FALSE;
CREATE INDEX shop_walmart_marketplace_events_by_timestamp ON mock.shop_walmart_marketplace_events (shop_id, event_timestamp);
CREATE INDEX shop_walmart_marketplace_events_by_timestamp_unprocessed ON mock.shop_walmart_marketplace_events (shop_id, event_timestamp) WHERE NOT processed;

ALTER TABLE mock.shop_wix_events
	ADD COLUMN processed BOOLEAN DEFAULT FALSE,
	ADD COLUMN processed_successfully BOOLEAN DEFAULT FALSE;
CREATE INDEX shop_wix_events_by_timestamp ON mock.shop_wix_events (shop_id, event_timestamp);
CREATE INDEX shop_wix_events_by_timestamp_unprocessed ON mock.shop_wix_events (shop_id, event_timestamp) WHERE NOT processed;

ALTER TABLE mock.shop_woo_commerce_events
	ADD COLUMN processed BOOLEAN DEFAULT FALSE,
	ADD COLUMN processed_successfully BOOLEAN DEFAULT FALSE;
CREATE INDEX shop_woo_commerce_events_by_timestamp ON mock.shop_woo_commerce_events (shop_id, event_timestamp);
CREATE INDEX shop_woo_commerce_events_by_timestamp_unprocessed ON mock.shop_woo_commerce_events (shop_id, event_timestamp) WHERE NOT processed;

ALTER TABLE mock.shop_zoho_events
	ADD COLUMN processed BOOLEAN DEFAULT FALSE,
	ADD COLUMN processed_successfully BOOLEAN DEFAULT FALSE;
CREATE INDEX shop_zoho_events_by_timestamp ON mock.shop_zoho_events (shop_id, event_timestamp);
CREATE INDEX shop_zoho_events_by_timestamp_unprocessed ON mock.shop_zoho_events (shop_id, event_timestamp) WHERE NOT processed;


--- event processing

CREATE OR REPLACE FUNCTION mock.process_raw_amazon_event() RETURNS TRIGGER AS $process_raw_amazon_event$
	BEGIN
		INSERT INTO mock.shop_amazon_events (
			platform,
			shop_id,
			event_timestamp,
			event_id
		)
		SELECT
			NEW.platform,
			NEW.shop_id,
			NEW.event_timestamp,
			NEW.event_id;

		PERFORM pg_notify('mock_shop_amazon_event_inserted', null);

		RETURN NULL;
	END;
$process_raw_amazon_event$ LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION mock.process_raw_big_cartel_event() RETURNS TRIGGER AS $process_raw_big_cartel_event$
	BEGIN
		INSERT INTO mock.shop_big_cartel_events (
			platform,
			shop_id,
			event_timestamp,
			event_id
		)
		SELECT
			NEW.platform,
			NEW.shop_id,
			NEW.event_timestamp,
			NEW.event_id;

		PERFORM pg_notify('mock_shop_big_cartel_event_inserted', null);

		RETURN NULL;
	END;
$process_raw_big_cartel_event$ LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION mock.process_raw_ebay_event() RETURNS TRIGGER AS $process_raw_ebay_event$
	BEGIN
		INSERT INTO mock.shop_ebay_events (
			platform,
			shop_id,
			event_timestamp,
			event_id
		)
		SELECT
			NEW.platform,
			NEW.shop_id,
			NEW.event_timestamp,
			NEW.event_id;

		PERFORM pg_notify('mock_shop_ebay_event_inserted', null);

		RETURN NULL;
	END;
$process_raw_ebay_event$ LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION mock.process_raw_ecwid_event() RETURNS TRIGGER AS $process_raw_ecwid_event$
	BEGIN
		INSERT INTO mock.shop_ecwid_events (
			platform,
			shop_id,
			event_timestamp,
			event_id
		)
		SELECT
			NEW.platform,
			NEW.shop_id,
			NEW.event_timestamp,
			NEW.event_id;

		PERFORM pg_notify('mock_shop_ecwid_event_inserted', null);

		RETURN NULL;
	END;
$process_raw_ecwid_event$ LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION mock.process_raw_etsy_event() RETURNS TRIGGER AS $process_raw_etsy_event$
	BEGIN
		INSERT INTO mock.shop_etsy_events (
			platform,
			shop_id,
			event_timestamp,
			event_id
		)
		SELECT
			NEW.platform,
			NEW.shop_id,
			NEW.event_timestamp,
			NEW.event_id;

		PERFORM pg_notify('mock_shop_etsy_event_inserted', null);

		RETURN NULL;
	END;
$process_raw_etsy_event$ LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION mock.process_raw_shopify_event() RETURNS TRIGGER AS $process_raw_shopify_event$
	BEGIN
		INSERT INTO mock.shop_shopify_events (
			platform,
			shop_id,
			event_timestamp,
			event_id
		)
		SELECT
			NEW.platform,
			NEW.shop_id,
			NEW.event_timestamp,
			NEW.event_id;

		PERFORM pg_notify('mock_shop_shopify_event_inserted', null);

		RETURN NULL;
	END;
$process_raw_shopify_event$ LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION mock.process_raw_square_online_event() RETURNS TRIGGER AS $process_raw_square_online_event$
	BEGIN
		INSERT INTO mock.shop_square_online_events (
			platform,
			shop_id,
			event_timestamp,
			event_id
		)
		SELECT
			NEW.platform,
			NEW.shop_id,
			NEW.event_timestamp,
			NEW.event_id;

		PERFORM pg_notify('mock_shop_square_online_event_inserted', null);

		RETURN NULL;
	END;
$process_raw_square_online_event$ LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION mock.process_raw_squarespace_event() RETURNS TRIGGER AS $process_raw_squarespace_event$
	BEGIN
		INSERT INTO mock.shop_squarespace_events (
			platform,
			shop_id,
			event_timestamp,
			event_id
		)
		SELECT
			NEW.platform,
			NEW.shop_id,
			NEW.event_timestamp,
			NEW.event_id;

		PERFORM pg_notify('mock_shop_squarespace_event_inserted', null);

		RETURN NULL;
	END;
$process_raw_squarespace_event$ LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION mock.process_raw_tiktok_event() RETURNS TRIGGER AS $process_raw_tiktok_event$
	BEGIN
		INSERT INTO mock.shop_tiktok_events (
			platform,
			shop_id,
			event_timestamp,
			event_id
		)
		SELECT
			NEW.platform,
			NEW.shop_id,
			NEW.event_timestamp,
			NEW.event_id;

		PERFORM pg_notify('mock_shop_tiktok_event_inserted', null);

		RETURN NULL;
	END;
$process_raw_tiktok_event$ LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION mock.process_raw_walmart_marketplace_event() RETURNS TRIGGER AS $process_raw_walmart_marketplace_event$
	BEGIN
		INSERT INTO mock.shop_walmart_marketplace_events (
			platform,
			shop_id,
			event_timestamp,
			event_id
		)
		SELECT
			NEW.platform,
			NEW.shop_id,
			NEW.event_timestamp,
			NEW.event_id;

		PERFORM pg_notify('mock_shop_walmart_marketplace_event_inserted', null);

		RETURN NULL;
	END;
$process_raw_walmart_marketplace_event$ LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION mock.process_raw_wix_event() RETURNS TRIGGER AS $process_raw_wix_event$
	BEGIN
		INSERT INTO mock.shop_wix_events (
			platform,
			shop_id,
			event_timestamp,
			event_id
		)
		SELECT
			NEW.platform,
			NEW.shop_id,
			NEW.event_timestamp,
			NEW.event_id;

		PERFORM pg_notify('mock_shop_wix_event_inserted', null);

		RETURN NULL;
	END;
$process_raw_wix_event$ LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION mock.process_raw_woo_commerce_event() RETURNS TRIGGER AS $process_raw_woo_commerce_event$
	BEGIN
		INSERT INTO mock.shop_woo_commerce_events (
			platform,
			shop_id,
			event_timestamp,
			event_id
		)
		SELECT
			NEW.platform,
			NEW.shop_id,
			NEW.event_timestamp,
			NEW.event_id;

		PERFORM pg_notify('mock_shop_woo_commerce_event_inserted', null);

		RETURN NULL;
	END;
$process_raw_woo_commerce_event$ LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION mock.process_raw_zoho_event() RETURNS TRIGGER AS $process_raw_zoho_event$
	BEGIN
		INSERT INTO mock.shop_zoho_events (
			platform,
			shop_id,
			event_timestamp,
			event_id
		)
		SELECT
			NEW.platform,
			NEW.shop_id,
			NEW.event_timestamp,
			NEW.event_id;

		PERFORM pg_notify('mock_shop_zoho_event_inserted', null);

		RETURN NULL;
	END;
$process_raw_zoho_event$ LANGUAGE plpgsql;

COMMIT;
