-- V032: Platform-neutral wizard drafts (issue #112).
-- Adds the platform discriminator and switches owner/chat/thread/message
-- columns from numeric to TEXT so the same table can hold both Telegram
-- ids (long) and Discord snowflakes (ulong). All conversions are safe:
-- the affected columns are nullable except chat_id/owner_telegram_id
-- which we cast via TEXT.

ALTER TABLE wizard_drafts
    ADD COLUMN platform TEXT NOT NULL DEFAULT 'Telegram';

-- Convert chat_id: BIGINT → TEXT. Existing rows hold Telegram chat ids
-- which convert losslessly to their decimal string form.
ALTER TABLE wizard_drafts
    ALTER COLUMN chat_id TYPE TEXT USING chat_id::TEXT;

-- Convert message_thread_id: INT (nullable) → TEXT (nullable).
ALTER TABLE wizard_drafts
    ALTER COLUMN message_thread_id TYPE TEXT USING message_thread_id::TEXT;

-- Convert draft_message_id: BIGINT (nullable) → TEXT (nullable).
ALTER TABLE wizard_drafts
    ALTER COLUMN draft_message_id TYPE TEXT USING draft_message_id::TEXT;

-- Rename owner_telegram_id → owner_id (now platform-agnostic) and
-- convert from BIGINT to TEXT.
ALTER TABLE wizard_drafts
    RENAME COLUMN owner_telegram_id TO owner_id;

ALTER TABLE wizard_drafts
    ALTER COLUMN owner_id TYPE TEXT USING owner_id::TEXT;

-- Replace the old owner lookup index with one that uses the new column
-- names and the platform discriminator.
DROP INDEX IF EXISTS idx_wizard_drafts_owner;

CREATE INDEX idx_wizard_drafts_owner
    ON wizard_drafts(platform, owner_id);

CREATE INDEX idx_wizard_drafts_platform
    ON wizard_drafts(platform);
