-- Purchase task configurations
CREATE TABLE purchase_configs (
    id SERIAL PRIMARY KEY,
    user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX idx_purchase_configs_user_id ON purchase_configs(user_id);

-- Purchase config board/group associations
CREATE TABLE purchase_config_boards (
    id SERIAL PRIMARY KEY,
    purchase_config_id INTEGER NOT NULL REFERENCES purchase_configs(id) ON DELETE CASCADE,
    board_id INTEGER NOT NULL REFERENCES shopping_boards(id) ON DELETE CASCADE,
    group_name VARCHAR(255),
    UNIQUE (purchase_config_id, board_id, group_name)
);

CREATE INDEX idx_purchase_config_boards_config_id ON purchase_config_boards(purchase_config_id);
CREATE INDEX idx_purchase_config_boards_board_id ON purchase_config_boards(board_id);

-- Add purchase_config_id to tasks
ALTER TABLE tasks ADD COLUMN purchase_config_id INTEGER REFERENCES purchase_configs(id) ON DELETE SET NULL;
CREATE INDEX idx_tasks_purchase_config_id ON tasks(purchase_config_id);
