-- Store SMS events linked to contacts for unified contact communication history

CREATE TABLE IF NOT EXISTS sms_messages (
    id SERIAL PRIMARY KEY,
    kontakt_id INTEGER NOT NULL REFERENCES contacts(id) ON DELETE CASCADE,
    bruger_id INTEGER REFERENCES users(user_id) ON DELETE SET NULL,
    recipient VARCHAR(64) NOT NULL,
    sender VARCHAR(64),
    message TEXT NOT NULL,
    status VARCHAR(16) NOT NULL DEFAULT 'sent',
    provider_response JSONB,
    created_at TIMESTAMP NOT NULL DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_sms_messages_kontakt_id ON sms_messages(kontakt_id);
CREATE INDEX IF NOT EXISTS idx_sms_messages_created_at ON sms_messages(created_at DESC);
