-- Shopping boards (аналог wishlist_boards)
CREATE TABLE shopping_boards (
    id SERIAL PRIMARY KEY,
    owner_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    name VARCHAR(255) NOT NULL,
    invite_token VARCHAR(64) UNIQUE,
    invite_enabled BOOLEAN DEFAULT FALSE,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    deleted BOOLEAN DEFAULT FALSE
);

CREATE INDEX idx_shopping_boards_owner_id ON shopping_boards(owner_id);
CREATE INDEX idx_shopping_boards_invite_token ON shopping_boards(invite_token) WHERE invite_token IS NOT NULL;
CREATE INDEX idx_shopping_boards_owner_deleted ON shopping_boards(owner_id, deleted);

-- Shopping board members (аналог wishlist_board_members)
CREATE TABLE shopping_board_members (
    id SERIAL PRIMARY KEY,
    board_id INTEGER NOT NULL REFERENCES shopping_boards(id) ON DELETE CASCADE,
    user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    joined_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT unique_shopping_board_member UNIQUE (board_id, user_id)
);

CREATE INDEX idx_shopping_board_members_board_id ON shopping_board_members(board_id);
CREATE INDEX idx_shopping_board_members_user_id ON shopping_board_members(user_id);

-- Shopping items (товары)
CREATE TABLE shopping_items (
    id SERIAL PRIMARY KEY,
    user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    board_id INTEGER NOT NULL REFERENCES shopping_boards(id) ON DELETE CASCADE,
    author_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    name VARCHAR(255) NOT NULL,
    group_name VARCHAR(255),
    volume_base NUMERIC(10,4) NOT NULL DEFAULT 1,
    repetition_period INTERVAL,
    next_show_at TIMESTAMP WITH TIME ZONE,
    completed INTEGER DEFAULT 0,
    last_completed_at TIMESTAMP WITH TIME ZONE,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    deleted BOOLEAN DEFAULT FALSE
);

CREATE INDEX idx_shopping_items_board_id ON shopping_items(board_id);
CREATE INDEX idx_shopping_items_user_id ON shopping_items(user_id);
CREATE INDEX idx_shopping_items_deleted ON shopping_items(deleted);
CREATE INDEX idx_shopping_items_next_show_at ON shopping_items(next_show_at);
