-- Migration 018: Opportunity Comments
-- Adds a lightweight discussion thread for each pipeline opportunity

CREATE TABLE IF NOT EXISTS pipeline_opportunity_comments (
    id SERIAL PRIMARY KEY,
    opportunity_id INTEGER NOT NULL REFERENCES pipeline_opportunities(id) ON DELETE CASCADE,
    user_id INTEGER REFERENCES users(user_id) ON DELETE SET NULL,
    author_name VARCHAR(255) NOT NULL DEFAULT 'Hub Bruger',
    content TEXT NOT NULL,
    email_id INTEGER REFERENCES email_messages(id) ON DELETE SET NULL,
    contract_number VARCHAR(100),
    contract_context TEXT,
    contract_link TEXT,
    metadata JSONB,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX IF NOT EXISTS idx_pipeline_opportunity_comments_opportunity_id ON pipeline_opportunity_comments(opportunity_id);
CREATE INDEX IF NOT EXISTS idx_pipeline_opportunity_comments_email_id ON pipeline_opportunity_comments(email_id);
CREATE INDEX IF NOT EXISTS idx_pipeline_opportunity_comments_contract_number ON pipeline_opportunity_comments(contract_number);

CREATE OR REPLACE FUNCTION update_pipeline_opportunity_comments_updated_at()
RETURNS TRIGGER AS $$
BEGIN
    NEW.updated_at = CURRENT_TIMESTAMP;
    RETURN NEW;
END;
$$ LANGUAGE plpgsql;

DROP TRIGGER IF EXISTS trigger_pipeline_opportunity_comments_updated_at ON pipeline_opportunity_comments;
CREATE TRIGGER trigger_pipeline_opportunity_comments_updated_at
    BEFORE UPDATE ON pipeline_opportunity_comments
    FOR EACH ROW
    EXECUTE FUNCTION update_pipeline_opportunity_comments_updated_at();

COMMENT ON TABLE pipeline_opportunity_comments IS 'Comments, emails, and contract context captured from the pipeline detail view.';
