-- seed_m3.sql
-- M3 seed. Adds the per-company telegram_bots row, sets
-- telegram_invite_code on every individual, and **pre-links**
-- Alice to a fake Telegram account so the smoke test can
-- exercise the full M3 path (FCM + Telegram delivery) on
-- the very first POST.
--
-- Bob and Carol get invite codes but are NOT linked. The
-- smoke test simulates `/start <code>` via faketgmd's
-- /admin/queue endpoint, watches the link happen, then
-- continues.
--
-- Idempotent (ON CONFLICT DO UPDATE for the link, ON
-- CONFLICT DO NOTHING for the rest). Safe to re-run.

-- ── telegram_bots ────────────────────────────────────────────────
-- One bot for acme-001. The bot_token is the faketgmd sentinel
-- "fake-tg-bot-token-acme-001". In production this is a real
-- token from @BotFather; see M3_VERIFICATION §Step 8.
INSERT INTO telegram_bots (bot_id, company_id, name, bot_token) VALUES
    ('primary', 'acme-001', 'Acme Ops', 'fake-tg-bot-token-acme-001')
ON CONFLICT (company_id, bot_id) DO NOTHING;

-- ── individuals: invite codes + Alice's pre-link ───────────────
-- All three individuals get an invite code. Re-running the
-- seed is safe: the code is overwritten on conflict.
UPDATE individuals SET telegram_invite_code = 'acme-alice-001' WHERE id = 'ind-acme-001';
UPDATE individuals SET telegram_invite_code = 'acme-bob-002'   WHERE id = 'ind-acme-002';
UPDATE individuals SET telegram_invite_code = 'acme-carol-003' WHERE id = 'ind-acme-003';

-- Alice is already linked. The faketgmd admin endpoint can
-- override these later; for M3 verification step 2 we want
-- her to be linked so the first POST already exercises
-- both fcm and telegram delivery paths.
UPDATE individuals
   SET telegram_chat_id = 1001,
       telegram_user_id = 900001
 WHERE id = 'ind-acme-001';

-- ── channel_mask expansion ─────────────────────────────────────
-- Bob's subscription was ['fcm'] in M2. M3 needs it to be
-- ['fcm','telegram'] so that, once he's linked via /start,
-- the resolver starts emitting telegram endpoints for him.
-- For step 2 the existing fcm path still fires for him; the
-- telegram path is no-op until step 5 (the /start sim).
UPDATE subscriptions
   SET channel_mask = '["fcm","telegram"]'::jsonb
 WHERE company_id = 'acme-001'
   AND individual_id IN ('ind-acme-001','ind-acme-002','ind-acme-003');

-- Carol stays with channel_mask = ['fcm','telegram'] too, so
-- the same channel expansion is exercised. Her quiet_hours
-- keep her out of any non-inminent_colapse alert regardless.
