BEGIN;


-- new mock schema

CREATE SCHEMA mock;


-- mock accounts

CREATE TABLE mock.accounts (
	account_id 		SERIAL		PRIMARY KEY,
	user_id			TEXT		NOT NULL		REFERENCES oauth_users (user_id)
);
CREATE UNIQUE INDEX ON mock.accounts (user_id);
CREATE UNIQUE INDEX ON mock.accounts (account_id, user_id);


-- mock shops

CREATE TABLE mock.shop_tiktok (
	user_id			TEXT		NOT NULL		REFERENCES oauth_users (user_id),
	account_id 		INTEGER		NOT NULL		REFERENCES mock.accounts (account_id),
	shop_id			TEXT		NOT NULL,
	name			TEXT		NOT NULL		CHECK (char_length(name) > 0),

	PRIMARY KEY (account_id, shop_id),
	FOREIGN KEY (account_id, user_id) REFERENCES mock.accounts (account_id, user_id)
);

CREATE TABLE mock.shop_shopify (
	user_id			TEXT		NOT NULL 		REFERENCES oauth_users (user_id),
	account_id		INTEGER		NOT NULL		REFERENCES mock.accounts (account_id),
	shop_id			TEXT		NOT NULL,
	name			TEXT		NOT NULL		CHECK (char_length(name) > 0),

	PRIMARY KEY (account_id, shop_id),
	FOREIGN KEY (account_id, user_id) REFERENCES mock.accounts (account_id, user_id)
);

CREATE TABLE mock.shop_woo_commerce (
	user_id			TEXT		NOT NULL 		REFERENCES oauth_users (user_id),
	account_id		INTEGER		NOT NULL		REFERENCES mock.accounts (account_id),
	shop_id			TEXT		NOT NULL,
	name			TEXT		NOT NULL		CHECK (char_length(name) > 0),

	PRIMARY KEY (account_id, shop_id),
	FOREIGN KEY (account_id, user_id) REFERENCES mock.accounts (account_id, user_id)
);

CREATE TABLE mock.shop_wix (
	user_id			TEXT		NOT NULL 		REFERENCES oauth_users (user_id),
	account_id		INTEGER		NOT NULL		REFERENCES mock.accounts (account_id),
	shop_id			TEXT		NOT NULL,
	name			TEXT		NOT NULL		CHECK (char_length(name) > 0),

	PRIMARY KEY (account_id, shop_id),
	FOREIGN KEY (account_id, user_id) REFERENCES mock.accounts (account_id, user_id)
);

CREATE TABLE mock.shop_squarespace (
	user_id			TEXT		NOT NULL 		REFERENCES oauth_users (user_id),
	account_id		INTEGER		NOT NULL		REFERENCES mock.accounts (account_id),
	shop_id			TEXT		NOT NULL,
	name			TEXT		NOT NULL		CHECK (char_length(name) > 0),

	PRIMARY KEY (account_id, shop_id),
	FOREIGN KEY (account_id, user_id) REFERENCES mock.accounts (account_id, user_id)
);

CREATE TABLE mock.shop_square_online (
	user_id			TEXT		NOT NULL 		REFERENCES oauth_users (user_id),
	account_id		INTEGER		NOT NULL		REFERENCES mock.accounts (account_id),
	shop_id			TEXT		NOT NULL,
	name			TEXT		NOT NULL		CHECK (char_length(name) > 0),

	PRIMARY KEY (account_id, shop_id),
	FOREIGN KEY (account_id, user_id) REFERENCES mock.accounts (account_id, user_id)
);

CREATE TABLE mock.shop_zoho (
	user_id			TEXT		NOT NULL 		REFERENCES oauth_users (user_id),
	account_id		INTEGER		NOT NULL		REFERENCES mock.accounts (account_id),
	shop_id			TEXT		NOT NULL,
	name			TEXT		NOT NULL		CHECK (char_length(name) > 0),

	PRIMARY KEY (account_id, shop_id),
	FOREIGN KEY (account_id, user_id) REFERENCES mock.accounts (account_id, user_id)
);

CREATE TABLE mock.shop_ecwid (
	user_id			TEXT		NOT NULL 		REFERENCES oauth_users (user_id),
	account_id		INTEGER		NOT NULL		REFERENCES mock.accounts (account_id),
	shop_id			TEXT		NOT NULL,
	name			TEXT		NOT NULL		CHECK (char_length(name) > 0),

	PRIMARY KEY (account_id, shop_id),
	FOREIGN KEY (account_id, user_id) REFERENCES mock.accounts (account_id, user_id)
);

CREATE TABLE mock.shop_big_cartel (
	user_id			TEXT		NOT NULL 		REFERENCES oauth_users (user_id),
	account_id		INTEGER		NOT NULL		REFERENCES mock.accounts (account_id),
	shop_id			TEXT		NOT NULL,
	name			TEXT		NOT NULL		CHECK (char_length(name) > 0),

	PRIMARY KEY (account_id, shop_id),
	FOREIGN KEY (account_id, user_id) REFERENCES mock.accounts (account_id, user_id)
);

CREATE TABLE mock.shop_amazon (
	user_id			TEXT		NOT NULL 		REFERENCES oauth_users (user_id),
	account_id		INTEGER		NOT NULL		REFERENCES mock.accounts (account_id),
	shop_id			TEXT		NOT NULL,
	name			TEXT		NOT NULL		CHECK (char_length(name) > 0),

	PRIMARY KEY (account_id, shop_id),
	FOREIGN KEY (account_id, user_id) REFERENCES mock.accounts (account_id, user_id)
);

CREATE TABLE mock.shop_walmart_marketplace (
	user_id			TEXT		NOT NULL 		REFERENCES oauth_users (user_id),
	account_id		INTEGER		NOT NULL		REFERENCES mock.accounts (account_id),
	shop_id			TEXT		NOT NULL,
	name			TEXT		NOT NULL		CHECK (char_length(name) > 0),

	PRIMARY KEY (account_id, shop_id),
	FOREIGN KEY (account_id, user_id) REFERENCES mock.accounts (account_id, user_id)
);

CREATE TABLE mock.shop_ebay (
	user_id			TEXT		NOT NULL 		REFERENCES oauth_users (user_id),
	account_id		INTEGER		NOT NULL		REFERENCES mock.accounts (account_id),
	shop_id			TEXT		NOT NULL,
	name			TEXT		NOT NULL		CHECK (char_length(name) > 0),

	PRIMARY KEY (account_id, shop_id),
	FOREIGN KEY (account_id, user_id) REFERENCES mock.accounts (account_id, user_id)
);


-- mock sync group listings

CREATE TABLE mock.sync_groups (
	sync_group_id		SERIAL		PRIMARY KEY,
	account_id			INTEGER		NOT NULL 		REFERENCES mock.accounts (account_id),
	name				TEXT		NOT NULL		CHECK (char_length(name) > 0)
);

CREATE TABLE mock.sync_group_listing_order_indexes (
	sync_group_id		INTEGER		NOT NULL	REFERENCES mock.sync_groups (sync_group_id),
	order_index			INTEGER		NOT NULL,

	UNIQUE (sync_group_id, order_index)
);


-- mock tiktok sync group listings

CREATE TABLE mock.shop_tiktok_listings (
	account_id			INTEGER		NOT NULL		REFERENCES mock.accounts (account_id),
	shop_id				TEXT		NOT NULL,
	listing_id			TEXT		NOT NULL,
	name				TEXT		NOT NULL,

	PRIMARY KEY (shop_id, listing_id),
	FOREIGN KEY (account_id, shop_id) REFERENCES mock.shop_tiktok (account_id, shop_id)
);

CREATE TABLE mock.shop_tiktok_sync_group_listings (
	account_id			INTEGER		NOT NULL		REFERENCES mock.accounts (account_id),
	sync_group_id		INTEGER		NOT NULL		REFERENCES mock.sync_groups (sync_group_id),
	shop_id				TEXT		NOT NULL,
	listing_id			TEXT		NOT NULL,
	order_index			INTEGER		NOT NULL,

	-- allow each shop to have at most ONE listing per sync group
	UNIQUE (sync_group_id, shop_id),
	FOREIGN KEY (account_id, shop_id) REFERENCES mock.shop_tiktok (account_id, shop_id),
	FOREIGN KEY (sync_group_id, order_index) REFERENCES mock.sync_group_listing_order_indexes (sync_group_id, order_index),
	FOREIGN KEY (shop_id, listing_id) REFERENCES mock.shop_tiktok_listings (shop_id, listing_id)
);

-- mock shopify sync group listings

CREATE TABLE mock.shop_shopify_listings (
	account_id			INTEGER		NOT NULL		REFERENCES mock.accounts (account_id),
	shop_id				TEXT		NOT NULL,
	listing_id			TEXT		NOT NULL,
	name				TEXT		NOT NULL,

	PRIMARY KEY (shop_id, listing_id),
	FOREIGN KEY (account_id, shop_id) REFERENCES mock.shop_shopify (account_id, shop_id)
);

CREATE TABLE mock.shop_shopify_sync_group_listings (
	account_id			INTEGER		NOT NULL		REFERENCES mock.accounts (account_id),
	sync_group_id		INTEGER		NOT NULL		REFERENCES mock.sync_groups (sync_group_id),
	shop_id				TEXT		NOT NULL,
	listing_id			TEXT		NOT NULL,
	order_index			INTEGER		NOT NULL,

	-- allow each shop to have at most ONE listing per sync group
	UNIQUE (sync_group_id, shop_id),
	FOREIGN KEY (account_id, shop_id) REFERENCES mock.shop_shopify (account_id, shop_id),
	FOREIGN KEY (sync_group_id, order_index) REFERENCES mock.sync_group_listing_order_indexes (sync_group_id, order_index),
	FOREIGN KEY (shop_id, listing_id) REFERENCES mock.shop_shopify_listings (shop_id, listing_id)
);

-- mock woo_commerce sync group listings

CREATE TABLE mock.shop_woo_commerce_listings (
	account_id			INTEGER		NOT NULL		REFERENCES mock.accounts (account_id),
	shop_id				TEXT		NOT NULL,
	listing_id			TEXT		NOT NULL,
	name				TEXT		NOT NULL,

	PRIMARY KEY (shop_id, listing_id),
	FOREIGN KEY (account_id, shop_id) REFERENCES mock.shop_woo_commerce (account_id, shop_id)
);

CREATE TABLE mock.shop_woo_commerce_sync_group_listings (
	account_id			INTEGER		NOT NULL		REFERENCES mock.accounts (account_id),
	sync_group_id		INTEGER		NOT NULL		REFERENCES mock.sync_groups (sync_group_id),
	shop_id				TEXT		NOT NULL,
	listing_id			TEXT		NOT NULL,
	order_index			INTEGER		NOT NULL,

	-- allow each shop to have at most ONE listing per sync group
	UNIQUE (sync_group_id, shop_id),
	FOREIGN KEY (account_id, shop_id) REFERENCES mock.shop_woo_commerce (account_id, shop_id),
	FOREIGN KEY (sync_group_id, order_index) REFERENCES mock.sync_group_listing_order_indexes (sync_group_id, order_index),
	FOREIGN KEY (shop_id, listing_id) REFERENCES mock.shop_woo_commerce_listings (shop_id, listing_id)
);

-- mock wix sync group listings

CREATE TABLE mock.shop_wix_listings (
	account_id			INTEGER		NOT NULL		REFERENCES mock.accounts (account_id),
	shop_id				TEXT		NOT NULL,
	listing_id			TEXT		NOT NULL,
	name				TEXT		NOT NULL,

	PRIMARY KEY (shop_id, listing_id),
	FOREIGN KEY (account_id, shop_id) REFERENCES mock.shop_wix (account_id, shop_id)
);

CREATE TABLE mock.shop_wix_sync_group_listings (
	account_id			INTEGER		NOT NULL		REFERENCES mock.accounts (account_id),
	sync_group_id		INTEGER		NOT NULL		REFERENCES mock.sync_groups (sync_group_id),
	shop_id				TEXT		NOT NULL,
	listing_id			TEXT		NOT NULL,
	order_index			INTEGER		NOT NULL,

	-- allow each shop to have at most ONE listing per sync group
	UNIQUE (sync_group_id, shop_id),
	FOREIGN KEY (account_id, shop_id) REFERENCES mock.shop_wix (account_id, shop_id),
	FOREIGN KEY (sync_group_id, order_index) REFERENCES mock.sync_group_listing_order_indexes (sync_group_id, order_index),
	FOREIGN KEY (shop_id, listing_id) REFERENCES mock.shop_wix_listings (shop_id, listing_id)
);

-- mock squarespace sync group listings

CREATE TABLE mock.shop_squarespace_listings (
	account_id			INTEGER		NOT NULL		REFERENCES mock.accounts (account_id),
	shop_id				TEXT		NOT NULL,
	listing_id			TEXT		NOT NULL,
	name				TEXT		NOT NULL,

	PRIMARY KEY (shop_id, listing_id),
	FOREIGN KEY (account_id, shop_id) REFERENCES mock.shop_squarespace (account_id, shop_id)
);

CREATE TABLE mock.shop_squarespace_sync_group_listings (
	account_id			INTEGER		NOT NULL		REFERENCES mock.accounts (account_id),
	sync_group_id		INTEGER		NOT NULL		REFERENCES mock.sync_groups (sync_group_id),
	shop_id				TEXT		NOT NULL,
	listing_id			TEXT		NOT NULL,
	order_index			INTEGER		NOT NULL,

	-- allow each shop to have at most ONE listing per sync group
	UNIQUE (sync_group_id, shop_id),
	FOREIGN KEY (account_id, shop_id) REFERENCES mock.shop_squarespace (account_id, shop_id),
	FOREIGN KEY (sync_group_id, order_index) REFERENCES mock.sync_group_listing_order_indexes (sync_group_id, order_index),
	FOREIGN KEY (shop_id, listing_id) REFERENCES mock.shop_squarespace_listings (shop_id, listing_id)
);

-- mock square_online sync group listings

CREATE TABLE mock.shop_square_online_listings (
	account_id			INTEGER		NOT NULL		REFERENCES mock.accounts (account_id),
	shop_id				TEXT		NOT NULL,
	listing_id			TEXT		NOT NULL,
	name				TEXT		NOT NULL,

	PRIMARY KEY (shop_id, listing_id),
	FOREIGN KEY (account_id, shop_id) REFERENCES mock.shop_square_online (account_id, shop_id)
);

CREATE TABLE mock.shop_square_online_sync_group_listings (
	account_id			INTEGER		NOT NULL		REFERENCES mock.accounts (account_id),
	sync_group_id		INTEGER		NOT NULL		REFERENCES mock.sync_groups (sync_group_id),
	shop_id				TEXT		NOT NULL,
	listing_id			TEXT		NOT NULL,
	order_index			INTEGER		NOT NULL,

	-- allow each shop to have at most ONE listing per sync group
	UNIQUE (sync_group_id, shop_id),
	FOREIGN KEY (account_id, shop_id) REFERENCES mock.shop_square_online (account_id, shop_id),
	FOREIGN KEY (sync_group_id, order_index) REFERENCES mock.sync_group_listing_order_indexes (sync_group_id, order_index),
	FOREIGN KEY (shop_id, listing_id) REFERENCES mock.shop_square_online_listings (shop_id, listing_id)
);

-- mock zoho sync group listings

CREATE TABLE mock.shop_zoho_listings (
	account_id			INTEGER		NOT NULL		REFERENCES mock.accounts (account_id),
	shop_id				TEXT		NOT NULL,
	listing_id			TEXT		NOT NULL,
	name				TEXT		NOT NULL,

	PRIMARY KEY (shop_id, listing_id),
	FOREIGN KEY (account_id, shop_id) REFERENCES mock.shop_zoho (account_id, shop_id)
);

CREATE TABLE mock.shop_zoho_sync_group_listings (
	account_id			INTEGER		NOT NULL		REFERENCES mock.accounts (account_id),
	sync_group_id		INTEGER		NOT NULL		REFERENCES mock.sync_groups (sync_group_id),
	shop_id				TEXT		NOT NULL,
	listing_id			TEXT		NOT NULL,
	order_index			INTEGER		NOT NULL,

	-- allow each shop to have at most ONE listing per sync group
	UNIQUE (sync_group_id, shop_id),
	FOREIGN KEY (account_id, shop_id) REFERENCES mock.shop_zoho (account_id, shop_id),
	FOREIGN KEY (sync_group_id, order_index) REFERENCES mock.sync_group_listing_order_indexes (sync_group_id, order_index),
	FOREIGN KEY (shop_id, listing_id) REFERENCES mock.shop_zoho_listings (shop_id, listing_id)
);

-- mock ecwid sync group listings

CREATE TABLE mock.shop_ecwid_listings (
	account_id			INTEGER		NOT NULL		REFERENCES mock.accounts (account_id),
	shop_id				TEXT		NOT NULL,
	listing_id			TEXT		NOT NULL,
	name				TEXT		NOT NULL,

	PRIMARY KEY (shop_id, listing_id),
	FOREIGN KEY (account_id, shop_id) REFERENCES mock.shop_ecwid (account_id, shop_id)
);

CREATE TABLE mock.shop_ecwid_sync_group_listings (
	account_id			INTEGER		NOT NULL		REFERENCES mock.accounts (account_id),
	sync_group_id		INTEGER		NOT NULL		REFERENCES mock.sync_groups (sync_group_id),
	shop_id				TEXT		NOT NULL,
	listing_id			TEXT		NOT NULL,
	order_index			INTEGER		NOT NULL,

	-- allow each shop to have at most ONE listing per sync group
	UNIQUE (sync_group_id, shop_id),
	FOREIGN KEY (account_id, shop_id) REFERENCES mock.shop_ecwid (account_id, shop_id),
	FOREIGN KEY (sync_group_id, order_index) REFERENCES mock.sync_group_listing_order_indexes (sync_group_id, order_index),
	FOREIGN KEY (shop_id, listing_id) REFERENCES mock.shop_ecwid_listings (shop_id, listing_id)
);

-- mock big_cartel sync group listings

CREATE TABLE mock.shop_big_cartel_listings (
	account_id			INTEGER		NOT NULL		REFERENCES mock.accounts (account_id),
	shop_id				TEXT		NOT NULL,
	listing_id			TEXT		NOT NULL,
	name				TEXT		NOT NULL,

	PRIMARY KEY (shop_id, listing_id),
	FOREIGN KEY (account_id, shop_id) REFERENCES mock.shop_big_cartel (account_id, shop_id)
);

CREATE TABLE mock.shop_big_cartel_sync_group_listings (
	account_id			INTEGER		NOT NULL		REFERENCES mock.accounts (account_id),
	sync_group_id		INTEGER		NOT NULL		REFERENCES mock.sync_groups (sync_group_id),
	shop_id				TEXT		NOT NULL,
	listing_id			TEXT		NOT NULL,
	order_index			INTEGER		NOT NULL,

	-- allow each shop to have at most ONE listing per sync group
	UNIQUE (sync_group_id, shop_id),
	FOREIGN KEY (account_id, shop_id) REFERENCES mock.shop_big_cartel (account_id, shop_id),
	FOREIGN KEY (sync_group_id, order_index) REFERENCES mock.sync_group_listing_order_indexes (sync_group_id, order_index),
	FOREIGN KEY (shop_id, listing_id) REFERENCES mock.shop_big_cartel_listings (shop_id, listing_id)
);

-- mock amazon sync group listings

CREATE TABLE mock.shop_amazon_listings (
	account_id			INTEGER		NOT NULL		REFERENCES mock.accounts (account_id),
	shop_id				TEXT		NOT NULL,
	listing_id			TEXT		NOT NULL,
	name				TEXT		NOT NULL,

	PRIMARY KEY (shop_id, listing_id),
	FOREIGN KEY (account_id, shop_id) REFERENCES mock.shop_amazon (account_id, shop_id)
);

CREATE TABLE mock.shop_amazon_sync_group_listings (
	account_id			INTEGER		NOT NULL		REFERENCES mock.accounts (account_id),
	sync_group_id		INTEGER		NOT NULL		REFERENCES mock.sync_groups (sync_group_id),
	shop_id				TEXT		NOT NULL,
	listing_id			TEXT		NOT NULL,
	order_index			INTEGER		NOT NULL,

	-- allow each shop to have at most ONE listing per sync group
	UNIQUE (sync_group_id, shop_id),
	FOREIGN KEY (account_id, shop_id) REFERENCES mock.shop_amazon (account_id, shop_id),
	FOREIGN KEY (sync_group_id, order_index) REFERENCES mock.sync_group_listing_order_indexes (sync_group_id, order_index),
	FOREIGN KEY (shop_id, listing_id) REFERENCES mock.shop_amazon_listings (shop_id, listing_id)
);

-- mock walmart_marketplace sync group listings

CREATE TABLE mock.shop_walmart_marketplace_listings (
	account_id			INTEGER		NOT NULL		REFERENCES mock.accounts (account_id),
	shop_id				TEXT		NOT NULL,
	listing_id			TEXT		NOT NULL,
	name				TEXT		NOT NULL,

	PRIMARY KEY (shop_id, listing_id),
	FOREIGN KEY (account_id, shop_id) REFERENCES mock.shop_walmart_marketplace (account_id, shop_id)
);

CREATE TABLE mock.shop_walmart_marketplace_sync_group_listings (
	account_id			INTEGER		NOT NULL		REFERENCES mock.accounts (account_id),
	sync_group_id		INTEGER		NOT NULL		REFERENCES mock.sync_groups (sync_group_id),
	shop_id				TEXT		NOT NULL,
	listing_id			TEXT		NOT NULL,
	order_index			INTEGER		NOT NULL,

	-- allow each shop to have at most ONE listing per sync group
	UNIQUE (sync_group_id, shop_id),
	FOREIGN KEY (account_id, shop_id) REFERENCES mock.shop_walmart_marketplace (account_id, shop_id),
	FOREIGN KEY (sync_group_id, order_index) REFERENCES mock.sync_group_listing_order_indexes (sync_group_id, order_index),
	FOREIGN KEY (shop_id, listing_id) REFERENCES mock.shop_walmart_marketplace_listings (shop_id, listing_id)
);

-- mock ebay sync group listings

CREATE TABLE mock.shop_ebay_listings (
	account_id			INTEGER		NOT NULL		REFERENCES mock.accounts (account_id),
	shop_id				TEXT		NOT NULL,
	listing_id			TEXT		NOT NULL,
	name				TEXT		NOT NULL,

	PRIMARY KEY (shop_id, listing_id),
	FOREIGN KEY (account_id, shop_id) REFERENCES mock.shop_ebay (account_id, shop_id)
);

CREATE TABLE mock.shop_ebay_sync_group_listings (
	account_id			INTEGER		NOT NULL		REFERENCES mock.accounts (account_id),
	sync_group_id		INTEGER		NOT NULL		REFERENCES mock.sync_groups (sync_group_id),
	shop_id				TEXT		NOT NULL,
	listing_id			TEXT		NOT NULL,
	order_index			INTEGER		NOT NULL,

	-- allow each shop to have at most ONE listing per sync group
	UNIQUE (sync_group_id, shop_id),
	FOREIGN KEY (account_id, shop_id) REFERENCES mock.shop_ebay (account_id, shop_id),
	FOREIGN KEY (sync_group_id, order_index) REFERENCES mock.sync_group_listing_order_indexes (sync_group_id, order_index),
	FOREIGN KEY (shop_id, listing_id) REFERENCES mock.shop_ebay_listings (shop_id, listing_id)
);



GRANT ALL ON SCHEMA mock TO PUBLIC;



COMMIT;
