BEGIN;


-- raw shop events

CREATE TABLE mock.raw_shop_events (
	platform			TEXT 			NOT NULL,
	shop_id				TEXT 			NOT NULL,
	event_timestamp		TIMESTAMPTZ 	NOT NULL,
	event_id			TEXT 			NOT NULL,
	raw_payload			JSONB 			NOT NULL,

	PRIMARY KEY (platform, shop_id, event_id, event_timestamp)
);


-- processed event tables

CREATE TABLE mock.shop_amazon_events (
	platform			TEXT			NOT NULL DEFAULT 'amazon' CHECK (platform = 'amazon'),
	shop_id				TEXT			NOT NULL,
	event_timestamp		TIMESTAMPTZ		NOT NULL,
	event_id			TEXT			NOT NULL,

	PRIMARY KEY (shop_id, event_id, event_timestamp),
	FOREIGN KEY (platform, shop_id, event_id, event_timestamp) REFERENCES mock.raw_shop_events
);

CREATE TABLE mock.shop_big_cartel_events (
	platform			TEXT			NOT NULL DEFAULT 'big_cartel' CHECK (platform = 'big_cartel'),
	shop_id				TEXT			NOT NULL,
	event_timestamp		TIMESTAMPTZ		NOT NULL,
	event_id			TEXT			NOT NULL,

	PRIMARY KEY (shop_id, event_id, event_timestamp),
	FOREIGN KEY (platform, shop_id, event_id, event_timestamp) REFERENCES mock.raw_shop_events
);

CREATE TABLE mock.shop_ebay_events (
	platform			TEXT			NOT NULL DEFAULT 'ebay' CHECK (platform = 'ebay'),
	shop_id				TEXT			NOT NULL,
	event_timestamp		TIMESTAMPTZ		NOT NULL,
	event_id			TEXT			NOT NULL,

	PRIMARY KEY (shop_id, event_id, event_timestamp),
	FOREIGN KEY (platform, shop_id, event_id, event_timestamp) REFERENCES mock.raw_shop_events
);

CREATE TABLE mock.shop_ecwid_events (
	platform			TEXT			NOT NULL DEFAULT 'ecwid' CHECK (platform = 'ecwid'),
	shop_id				TEXT			NOT NULL,
	event_timestamp		TIMESTAMPTZ		NOT NULL,
	event_id			TEXT			NOT NULL,

	PRIMARY KEY (shop_id, event_id, event_timestamp),
	FOREIGN KEY (platform, shop_id, event_id, event_timestamp) REFERENCES mock.raw_shop_events
);

CREATE TABLE mock.shop_etsy_events (
	platform			TEXT			NOT NULL DEFAULT 'etsy' CHECK (platform = 'etsy'),
	shop_id				TEXT			NOT NULL,
	event_timestamp		TIMESTAMPTZ		NOT NULL,
	event_id			TEXT			NOT NULL,

	PRIMARY KEY (shop_id, event_id, event_timestamp),
	FOREIGN KEY (platform, shop_id, event_id, event_timestamp) REFERENCES mock.raw_shop_events
);

CREATE TABLE mock.shop_shopify_events (
	platform			TEXT			NOT NULL DEFAULT 'shopify' CHECK (platform = 'shopify'),
	shop_id				TEXT			NOT NULL,
	event_timestamp		TIMESTAMPTZ		NOT NULL,
	event_id			TEXT			NOT NULL,

	PRIMARY KEY (shop_id, event_id, event_timestamp),
	FOREIGN KEY (platform, shop_id, event_id, event_timestamp) REFERENCES mock.raw_shop_events
);

CREATE TABLE mock.shop_square_online_events (
	platform			TEXT			NOT NULL DEFAULT 'square_online' CHECK (platform = 'square_online'),
	shop_id				TEXT			NOT NULL,
	event_timestamp		TIMESTAMPTZ		NOT NULL,
	event_id			TEXT			NOT NULL,

	PRIMARY KEY (shop_id, event_id, event_timestamp),
	FOREIGN KEY (platform, shop_id, event_id, event_timestamp) REFERENCES mock.raw_shop_events
);

CREATE TABLE mock.shop_squarespace_events (
	platform			TEXT			NOT NULL DEFAULT 'squarespace' CHECK (platform = 'squarespace'),
	shop_id				TEXT			NOT NULL,
	event_timestamp		TIMESTAMPTZ		NOT NULL,
	event_id			TEXT			NOT NULL,

	PRIMARY KEY (shop_id, event_id, event_timestamp),
	FOREIGN KEY (platform, shop_id, event_id, event_timestamp) REFERENCES mock.raw_shop_events
);

CREATE TABLE mock.shop_tiktok_events (
	platform			TEXT			NOT NULL DEFAULT 'tiktok' CHECK (platform = 'tiktok'),
	shop_id				TEXT			NOT NULL,
	event_timestamp		TIMESTAMPTZ		NOT NULL,
	event_id			TEXT			NOT NULL,

	PRIMARY KEY (shop_id, event_id, event_timestamp),
	FOREIGN KEY (platform, shop_id, event_id, event_timestamp) REFERENCES mock.raw_shop_events
);

CREATE TABLE mock.shop_walmart_marketplace_events (
	platform			TEXT			NOT NULL DEFAULT 'walmart_marketplace' CHECK (platform = 'walmart_marketplace'),
	shop_id				TEXT			NOT NULL,
	event_timestamp		TIMESTAMPTZ		NOT NULL,
	event_id			TEXT			NOT NULL,

	PRIMARY KEY (shop_id, event_id, event_timestamp),
	FOREIGN KEY (platform, shop_id, event_id, event_timestamp) REFERENCES mock.raw_shop_events
);

CREATE TABLE mock.shop_wix_events (
	platform			TEXT			NOT NULL DEFAULT 'wix' CHECK (platform = 'wix'),
	shop_id				TEXT			NOT NULL,
	event_timestamp		TIMESTAMPTZ		NOT NULL,
	event_id			TEXT			NOT NULL,

	PRIMARY KEY (shop_id, event_id, event_timestamp),
	FOREIGN KEY (platform, shop_id, event_id, event_timestamp) REFERENCES mock.raw_shop_events
);

CREATE TABLE mock.shop_woo_commerce_events (
	platform			TEXT			NOT NULL DEFAULT 'woo_commerce' CHECK (platform = 'woo_commerce'),
	shop_id				TEXT			NOT NULL,
	event_timestamp		TIMESTAMPTZ		NOT NULL,
	event_id			TEXT			NOT NULL,

	PRIMARY KEY (shop_id, event_id, event_timestamp),
	FOREIGN KEY (platform, shop_id, event_id, event_timestamp) REFERENCES mock.raw_shop_events
);

CREATE TABLE mock.shop_zoho_events (
	platform			TEXT			NOT NULL DEFAULT 'zoho' CHECK (platform = 'zoho'),
	shop_id				TEXT			NOT NULL,
	event_timestamp		TIMESTAMPTZ		NOT NULL,
	event_id			TEXT			NOT NULL,

	PRIMARY KEY (shop_id, event_id, event_timestamp),
	FOREIGN KEY (platform, shop_id, event_id, event_timestamp) REFERENCES mock.raw_shop_events
);


--- 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;
		RETURN NULL;
	END;
$process_raw_amazon_event$ LANGUAGE plpgsql;

CREATE OR REPLACE TRIGGER amazon_store_events
	AFTER INSERT ON mock.raw_shop_events
	FOR EACH ROW
		WHEN (NEW.platform = 'amazon')
			EXECUTE FUNCTION mock.process_raw_amazon_event();


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;
		RETURN NULL;
	END;
$process_raw_big_cartel_event$ LANGUAGE plpgsql;

CREATE OR REPLACE TRIGGER big_cartel_store_events
	AFTER INSERT ON mock.raw_shop_events
	FOR EACH ROW
		WHEN (NEW.platform = 'big_cartel')
			EXECUTE FUNCTION mock.process_raw_big_cartel_event();


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;
		RETURN NULL;
	END;
$process_raw_ebay_event$ LANGUAGE plpgsql;

CREATE OR REPLACE TRIGGER ebay_store_events
	AFTER INSERT ON mock.raw_shop_events
	FOR EACH ROW
		WHEN (NEW.platform = 'ebay')
			EXECUTE FUNCTION mock.process_raw_ebay_event();


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;
		RETURN NULL;
	END;
$process_raw_ecwid_event$ LANGUAGE plpgsql;

CREATE OR REPLACE TRIGGER ecwid_store_events
	AFTER INSERT ON mock.raw_shop_events
	FOR EACH ROW
		WHEN (NEW.platform = 'ecwid')
			EXECUTE FUNCTION mock.process_raw_ecwid_event();


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;
		RETURN NULL;
	END;
$process_raw_etsy_event$ LANGUAGE plpgsql;

CREATE OR REPLACE TRIGGER etsy_store_events
	AFTER INSERT ON mock.raw_shop_events
	FOR EACH ROW
		WHEN (NEW.platform = 'etsy')
			EXECUTE FUNCTION mock.process_raw_etsy_event();


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;
		RETURN NULL;
	END;
$process_raw_shopify_event$ LANGUAGE plpgsql;

CREATE OR REPLACE TRIGGER shopify_store_events
	AFTER INSERT ON mock.raw_shop_events
	FOR EACH ROW
		WHEN (NEW.platform = 'shopify')
			EXECUTE FUNCTION mock.process_raw_shopify_event();


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;
		RETURN NULL;
	END;
$process_raw_square_online_event$ LANGUAGE plpgsql;

CREATE OR REPLACE TRIGGER square_online_store_events
	AFTER INSERT ON mock.raw_shop_events
	FOR EACH ROW
		WHEN (NEW.platform = 'square_online')
			EXECUTE FUNCTION mock.process_raw_square_online_event();


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;
		RETURN NULL;
	END;
$process_raw_squarespace_event$ LANGUAGE plpgsql;

CREATE OR REPLACE TRIGGER squarespace_store_events
	AFTER INSERT ON mock.raw_shop_events
	FOR EACH ROW
		WHEN (NEW.platform = 'squarespace')
			EXECUTE FUNCTION mock.process_raw_squarespace_event();


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;
		RETURN NULL;
	END;
$process_raw_tiktok_event$ LANGUAGE plpgsql;

CREATE OR REPLACE TRIGGER tiktok_store_events
	AFTER INSERT ON mock.raw_shop_events
	FOR EACH ROW
		WHEN (NEW.platform = 'tiktok')
			EXECUTE FUNCTION mock.process_raw_tiktok_event();


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;
		RETURN NULL;
	END;
$process_raw_walmart_marketplace_event$ LANGUAGE plpgsql;

CREATE OR REPLACE TRIGGER walmart_marketplace_store_events
	AFTER INSERT ON mock.raw_shop_events
	FOR EACH ROW
		WHEN (NEW.platform = 'walmart_marketplace')
			EXECUTE FUNCTION mock.process_raw_walmart_marketplace_event();


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;
		RETURN NULL;
	END;
$process_raw_wix_event$ LANGUAGE plpgsql;

CREATE OR REPLACE TRIGGER wix_store_events
	AFTER INSERT ON mock.raw_shop_events
	FOR EACH ROW
		WHEN (NEW.platform = 'wix')
			EXECUTE FUNCTION mock.process_raw_wix_event();


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;
		RETURN NULL;
	END;
$process_raw_woo_commerce_event$ LANGUAGE plpgsql;

CREATE OR REPLACE TRIGGER woo_commerce_store_events
	AFTER INSERT ON mock.raw_shop_events
	FOR EACH ROW
		WHEN (NEW.platform = 'woo_commerce')
			EXECUTE FUNCTION mock.process_raw_woo_commerce_event();


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;
		RETURN NULL;
	END;
$process_raw_zoho_event$ LANGUAGE plpgsql;

CREATE OR REPLACE TRIGGER zoho_store_events
	AFTER INSERT ON mock.raw_shop_events
	FOR EACH ROW
		WHEN (NEW.platform = 'zoho')
			EXECUTE FUNCTION mock.process_raw_zoho_event();


COMMIT;
