-- Ingredient compositions table
CREATE TABLE ingredient_compositions (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    organization_id UUID NOT NULL REFERENCES organizations(id) ON DELETE CASCADE,
    outlet_id UUID REFERENCES outlets(id) ON DELETE CASCADE,
    parent_ingredient_id UUID NOT NULL REFERENCES ingredients(id) ON DELETE CASCADE,
    child_ingredient_id UUID NOT NULL REFERENCES ingredients(id) ON DELETE CASCADE,
    quantity DECIMAL(12,3) NOT NULL,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);

-- Indexes
CREATE INDEX idx_ingredient_compositions_organization_id ON ingredient_compositions(organization_id);
CREATE INDEX idx_ingredient_compositions_outlet_id ON ingredient_compositions(outlet_id);
CREATE INDEX idx_ingredient_compositions_parent_ingredient_id ON ingredient_compositions(parent_ingredient_id);
CREATE INDEX idx_ingredient_compositions_child_ingredient_id ON ingredient_compositions(child_ingredient_id);
CREATE INDEX idx_ingredient_compositions_created_at ON ingredient_compositions(created_at);

-- Unique constraint to prevent duplicate compositions
CREATE UNIQUE INDEX idx_ingredient_compositions_unique ON ingredient_compositions(parent_ingredient_id, child_ingredient_id); 