CREATE TABLE users (
    id BIGSERIAL PRIMARY KEY,
    created_at TIMESTAMP WITH TIME ZONE NOT NULL,
    updated_at TIMESTAMP WITH TIME ZONE NULL,
    deleted_at TIMESTAMP WITH TIME ZONE NULL,
    uuid VARCHAR(36) NOT NULL UNIQUE
);

CREATE TABLE merch (
    id BIGSERIAL PRIMARY KEY,
    created_at TIMESTAMP WITH TIME ZONE NOT NULL,
    updated_at TIMESTAMP WITH TIME ZONE NULL,
    deleted_at TIMESTAMP WITH TIME ZONE NULL,
    merch_uuid VARCHAR(36) NOT NULL UNIQUE,
    user_id BIGINT NOT NULL,
    name VARCHAR(255) NULL,

    CONSTRAINT fk_merch_user_id FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);

CREATE TABLE merch_origins (
    id BIGSERIAL PRIMARY KEY,
    created_at TIMESTAMP WITH TIME ZONE NOT NULL,
    deleted_at TIMESTAMP WITH TIME ZONE NULL,
    name VARCHAR(255) NULL
);

CREATE TABLE merch_prices (
    id BIGSERIAL PRIMARY KEY,
    created_at TIMESTAMP WITH TIME ZONE NOT NULL,
    updated_at TIMESTAMP WITH TIME ZONE NULL,
    deleted_at TIMESTAMP WITH TIME ZONE NULL,
    merch_id INT NOT NULL,
    origin_id INT NOT NULL,
    price INT NOT NULL,

    CONSTRAINT fk_merch_prices_merch_merch_id FOREIGN KEY (merch_id) REFERENCES merch(id) ON DELETE CASCADE,
    CONSTRAINT fk_merch_prices_merch_origins_id FOREIGN KEY (origin_id) REFERENCES merch_origins(id) ON DELETE CASCADE
);

CREATE TABLE merch_extra_data (
    id BIGSERIAL PRIMARY KEY,
    created_at TIMESTAMP WITH TIME ZONE NOT NULL,
    updated_at TIMESTAMP WITH TIME ZONE NULL,
    deleted_at TIMESTAMP WITH TIME ZONE NULL,
    merch_id BIGINT NOT NULL,
    origin_id BIGINT NOT NULL,
    url TEXT DEFAULT NULL,

    CONSTRAINT fk_merch_extra_data_merch_id FOREIGN KEY (merch_id) REFERENCES merch(id) ON DELETE CASCADE,
    CONSTRAINT fk_merch_extra_data_merch_origins_id FOREIGN KEY (origin_id) REFERENCES merch_origins(id) ON DELETE CASCADE

);

CREATE UNIQUE INDEX idx_merch_extra_data_merch_origin_id ON merch_extra_data (merch_id, origin_id);