-- Create link table for Opportunity <-> Contacts (Many-to-Many)
CREATE TABLE IF NOT EXISTS pipeline_opportunity_contacts (
    opportunity_id INTEGER NOT NULL REFERENCES pipeline_opportunities(id) ON DELETE CASCADE,
    contact_id INTEGER NOT NULL REFERENCES contacts(id) ON DELETE CASCADE,
    role VARCHAR(100), -- optional role in this opportunity (e.g. "Decision Maker", "Influencer")
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    PRIMARY KEY (opportunity_id, contact_id)
);

-- Index for faster lookups
CREATE INDEX IF NOT EXISTS idx_pipeline_opportunity_contacts_opp ON pipeline_opportunity_contacts(opportunity_id);
