-- =============================================================================
-- MIGRATION: Greeting knowledge-base entry ("Hi"/"Hello"/"Hey" -> a warm,
-- personalized reply instead of the generic "I'm not sure about that one").
-- Run against: chat_bot, after 001-006 (needs chat.knowledge_entries) and
-- after whichever application row you're seeding this for already exists
-- (001/007 for the demo/production application).
--
-- Companion code change (chat-service/src/app/chat/scripted-chat-responder.ts,
-- chat-service/src/app/chat/retrieval/text.ts): "hi"/"hello"/"hey" used to be
-- filtered out as stopwords, so a bare greeting tokenized to nothing and
-- always fell through to the fallback message — this migration only helps
-- once that code is deployed too. `{{User}}` in the answer below is filled
-- in at reply time with the shopper's first name (or "there" if anonymous).
--
-- Idempotent — safe to re-run in any plain SQL client (no psql-only syntax).
-- Targets the seeded demo application ('app_demo_store') — if you're seeding
-- a different application, replace that literal in the INSERT below.
-- =============================================================================

BEGIN;

INSERT INTO "chat"."knowledge_entries"
    ("id", "applicationId", "question", "answer", "keywords", "links", "sortOrder", "updatedBy", "updatedOn")
VALUES
    (
        'kb_demo_greeting',
        'app_demo_store',
        'Hi',
        'Hello {{User}}! How can I assist you now?',
        ARRAY['hi','hello','hey','hii','helo','good morning','good afternoon','good evening','greetings']::TEXT[],
        '[]'::jsonb,
        0,
        'system',
        CURRENT_TIMESTAMP
    )
ON CONFLICT ("id") DO UPDATE SET
    "question"  = EXCLUDED."question",
    "answer"    = EXCLUDED."answer",
    "keywords"  = EXCLUDED."keywords",
    "links"     = EXCLUDED."links",
    "sortOrder" = EXCLUDED."sortOrder",
    "updatedBy" = 'system',
    "updatedOn" = CURRENT_TIMESTAMP;

COMMIT;
