-- +goose Up

CREATE TABLE menu_plans (
    id         UUID PRIMARY KEY DEFAULT uuid_generate_v7(),
    user_id    UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    week_start DATE NOT NULL,
    created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
    UNIQUE(user_id, week_start)
);

CREATE TABLE menu_items (
    id           UUID PRIMARY KEY DEFAULT uuid_generate_v7(),
    menu_plan_id UUID NOT NULL REFERENCES menu_plans(id) ON DELETE CASCADE,
    day_of_week  INT  NOT NULL CHECK (day_of_week BETWEEN 1 AND 7),
    meal_type    TEXT NOT NULL CHECK (meal_type IN ('breakfast','lunch','dinner')),
    recipe_id    UUID REFERENCES saved_recipes(id) ON DELETE SET NULL,
    recipe_data  JSONB,
    UNIQUE(menu_plan_id, day_of_week, meal_type)
);

-- Stores the generated shopping list for a menu plan.
-- items is a JSONB array of {name, category, amount, unit, checked, in_stock}.
CREATE TABLE shopping_lists (
    id           UUID PRIMARY KEY DEFAULT uuid_generate_v7(),
    user_id      UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    menu_plan_id UUID REFERENCES menu_plans(id) ON DELETE CASCADE,
    items        JSONB NOT NULL DEFAULT '[]',
    generated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
    UNIQUE(user_id, menu_plan_id)
);

-- +goose Down
DROP TABLE shopping_lists;
DROP TABLE menu_items;
DROP TABLE menu_plans;
