-- v1.4_whatsapp.sql — WhatsApp message history
-- Applied per-tenant database

CREATE TABLE IF NOT EXISTS whatsapp_messages (
    id SERIAL PRIMARY KEY,
    phone VARCHAR(20) NOT NULL,
    direction VARCHAR(10) NOT NULL,  -- 'incoming' or 'outgoing'
    message_text TEXT,
    message_type VARCHAR(20) DEFAULT 'text',
    wa_message_id VARCHAR(100),
    status VARCHAR(20) DEFAULT 'sent',
    related_type VARCHAR(50),  -- 'quotation', 'sale', 'alert'
    related_id INTEGER,
    created_at TIMESTAMPTZ DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_wa_messages_phone ON whatsapp_messages(phone);
CREATE INDEX IF NOT EXISTS idx_wa_messages_created ON whatsapp_messages(created_at DESC);
