CREATE TABLE learnings (
    guid UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    summary TEXT NOT NULL,
    details TEXT,
    category TEXT,
    area TEXT,
    status TEXT NOT NULL DEFAULT 'provisional',
    priority TEXT DEFAULT 'medium',
    confidence TEXT DEFAULT 'hypothesis',
    action_required BOOLEAN DEFAULT false,
    source_type TEXT,
    source_ref TEXT,
    project_id UUID REFERENCES projects(guid) ON DELETE SET NULL,
    related_thought_id UUID REFERENCES thoughts(guid) ON DELETE SET NULL,
    related_skill_id UUID REFERENCES skills(guid) ON DELETE SET NULL,
    reviewed_by TEXT,
    reviewed_at TIMESTAMP WITH TIME ZONE,
    duplicate_of UUID REFERENCES learnings(guid) ON DELETE SET NULL,
    supersedes UUID REFERENCES learnings(guid) ON DELETE SET NULL,
    tags TEXT[],
    created_at TIMESTAMP WITH TIME ZONE DEFAULT now(),
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT now()
);

CREATE INDEX idx_learnings_project_id ON learnings(project_id);
CREATE INDEX idx_learnings_category ON learnings(category);
CREATE INDEX idx_learnings_status ON learnings(status);
CREATE INDEX idx_learnings_tags ON learnings USING GIN(tags);
CREATE INDEX idx_learnings_search ON learnings USING GIN(to_tsvector('simple', summary || ' ' || coalesce(details, '')));
