BEGIN;


-- 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 stores

CREATE TABLE mock_store_tiktok (
	user_id			TEXT		NOT NULL		REFERENCES oauth_users (user_id),
	account_id 		INTEGER		NOT NULL		REFERENCES mock_accounts (account_id),
	store_id		TEXT		PRIMARY KEY,
	name			TEXT		NOT NULL		CHECK (char_length(name) > 0),

	FOREIGN KEY (account_id, user_id) REFERENCES mock_accounts (account_id, user_id)
);

CREATE TABLE mock_store_shopify (
	user_id			TEXT		NOT NULL 		REFERENCES oauth_users (user_id),
	account_id		INTEGER		NOT NULL		REFERENCES mock_accounts (account_id),
	store_id		TEXT		PRIMARY KEY,
	name			TEXT		NOT NULL		CHECK (char_length(name) > 0),

	FOREIGN KEY (account_id, user_id) REFERENCES mock_accounts (account_id, user_id)
);

CREATE TABLE mock_store_woo_commerce (
	user_id			TEXT		NOT NULL 		REFERENCES oauth_users (user_id),
	account_id		INTEGER		NOT NULL		REFERENCES mock_accounts (account_id),
	store_id		TEXT		PRIMARY KEY,
	name			TEXT		NOT NULL		CHECK (char_length(name) > 0),

	FOREIGN KEY (account_id, user_id) REFERENCES mock_accounts (account_id, user_id)
);

CREATE TABLE mock_store_wix (
	user_id			TEXT		NOT NULL 		REFERENCES oauth_users (user_id),
	account_id		INTEGER		NOT NULL		REFERENCES mock_accounts (account_id),
	store_id		TEXT		PRIMARY KEY,
	name			TEXT		NOT NULL		CHECK (char_length(name) > 0),

	FOREIGN KEY (account_id, user_id) REFERENCES mock_accounts (account_id, user_id)
);

CREATE TABLE mock_store_squarespace (
	user_id			TEXT		NOT NULL 		REFERENCES oauth_users (user_id),
	account_id		INTEGER		NOT NULL		REFERENCES mock_accounts (account_id),
	store_id		TEXT		PRIMARY KEY,
	name			TEXT		NOT NULL		CHECK (char_length(name) > 0),

	FOREIGN KEY (account_id, user_id) REFERENCES mock_accounts (account_id, user_id)
);

CREATE TABLE mock_store_square_online (
	user_id			TEXT		NOT NULL 		REFERENCES oauth_users (user_id),
	account_id		INTEGER		NOT NULL		REFERENCES mock_accounts (account_id),
	store_id		TEXT		PRIMARY KEY,
	name			TEXT		NOT NULL		CHECK (char_length(name) > 0),

	FOREIGN KEY (account_id, user_id) REFERENCES mock_accounts (account_id, user_id)
);

CREATE TABLE mock_store_zoho (
	user_id			TEXT		NOT NULL 		REFERENCES oauth_users (user_id),
	account_id		INTEGER		NOT NULL		REFERENCES mock_accounts (account_id),
	store_id		TEXT		PRIMARY KEY,
	name			TEXT		NOT NULL		CHECK (char_length(name) > 0),

	FOREIGN KEY (account_id, user_id) REFERENCES mock_accounts (account_id, user_id)
);

CREATE TABLE mock_store_ecwid (
	user_id			TEXT		NOT NULL 		REFERENCES oauth_users (user_id),
	account_id		INTEGER		NOT NULL		REFERENCES mock_accounts (account_id),
	store_id		TEXT		PRIMARY KEY,
	name			TEXT		NOT NULL		CHECK (char_length(name) > 0),

	FOREIGN KEY (account_id, user_id) REFERENCES mock_accounts (account_id, user_id)
);

CREATE TABLE mock_store_big_cartel (
	user_id			TEXT		NOT NULL 		REFERENCES oauth_users (user_id),
	account_id		INTEGER		NOT NULL		REFERENCES mock_accounts (account_id),
	store_id		TEXT		PRIMARY KEY,
	name			TEXT		NOT NULL		CHECK (char_length(name) > 0),

	FOREIGN KEY (account_id, user_id) REFERENCES mock_accounts (account_id, user_id)
);

CREATE TABLE mock_store_amazon (
	user_id			TEXT		NOT NULL 		REFERENCES oauth_users (user_id),
	account_id		INTEGER		NOT NULL		REFERENCES mock_accounts (account_id),
	store_id		TEXT		PRIMARY KEY,
	name			TEXT		NOT NULL		CHECK (char_length(name) > 0),

	FOREIGN KEY (account_id, user_id) REFERENCES mock_accounts (account_id, user_id)
);

CREATE TABLE mock_store_walmart_marketplace (
	user_id			TEXT		NOT NULL 		REFERENCES oauth_users (user_id),
	account_id		INTEGER		NOT NULL		REFERENCES mock_accounts (account_id),
	store_id		TEXT		PRIMARY KEY,
	name			TEXT		NOT NULL		CHECK (char_length(name) > 0),

	FOREIGN KEY (account_id, user_id) REFERENCES mock_accounts (account_id, user_id)
);

CREATE TABLE mock_store_ebay (
	user_id			TEXT		NOT NULL 		REFERENCES oauth_users (user_id),
	account_id		INTEGER		NOT NULL		REFERENCES mock_accounts (account_id),
	store_id		TEXT		PRIMARY KEY,
	name			TEXT		NOT NULL		CHECK (char_length(name) > 0),

	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)
);

CREATE TABLE mock_sync_group_listings (
	sync_group_id		INTEGER		NOT NULL		REFERENCES mock_sync_groups (sync_group_id),
	order_index			INTEGER		NOT NULL,
	platform			platform	NOT NULL,
	shop_id				TEXT		NOT NULL,
	listing_id			TEXT		NOT NULL,

	PRIMARY KEY (sync_group_id, order_index)
);


COMMIT;
