-- ============================================================
-- Migration: 005_message_misc.sql
-- Tables:  message_notification, message_scheduled, message_sticker,
--          message_forward, message_component
-- ============================================================

BEGIN;

-- models/message_notification.rs  →  message_notification
CREATE TABLE IF NOT EXISTS message_notification (
    id            UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    message_id        UUID NOT NULL REFERENCES message(id) ON DELETE CASCADE,
    channel_id        UUID NOT NULL,
    user_id           UUID NOT NULL,
    reason            TEXT NOT NULL,
    status            TEXT NOT NULL DEFAULT 'pending',
    delivery_channel  TEXT NULL,
    delivered_at      TIMESTAMPTZ NULL,
    read_at           TIMESTAMPTZ NULL,
    created_at        TIMESTAMPTZ NOT NULL DEFAULT now()
);

CREATE INDEX IF NOT EXISTS idx_message_notification_user_id
    ON message_notification (user_id, created_at DESC);
CREATE INDEX IF NOT EXISTS idx_message_notification_status
    ON message_notification (status);

-- models/message_scheduled.rs  →  message_scheduled
CREATE TABLE IF NOT EXISTS message_scheduled (
    id            UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    channel_id            UUID NOT NULL,
    author_id             UUID NOT NULL,
    thread_id             UUID NULL REFERENCES message_thread(id) ON DELETE SET NULL,
    reply_to_message_id   UUID NULL REFERENCES message(id) ON DELETE SET NULL,
    body                  TEXT NOT NULL,
    metadata              JSONB NULL,
    scheduled_at          TIMESTAMPTZ NOT NULL,
    status                TEXT NOT NULL DEFAULT 'pending',
    sent_message_id       UUID NULL REFERENCES message(id) ON DELETE SET NULL,
    error                 TEXT NULL,
    created_at            TIMESTAMPTZ NOT NULL DEFAULT now(),
    updated_at            TIMESTAMPTZ NOT NULL DEFAULT now()
);

CREATE INDEX IF NOT EXISTS idx_message_scheduled_status_at
    ON message_scheduled (status, scheduled_at);

-- models/message_sticker.rs  →  message_sticker
CREATE TABLE IF NOT EXISTS message_sticker (
    id            UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    message_id   UUID NOT NULL REFERENCES message(id) ON DELETE CASCADE,
    sticker_id   UUID NOT NULL,
    name         TEXT NOT NULL,
    image_url    TEXT NOT NULL,
    format_type  TEXT NOT NULL DEFAULT 'png',
    pack_name    TEXT NULL,
    tags         TEXT NULL,
    created_at   TIMESTAMPTZ NOT NULL DEFAULT now()
);

CREATE INDEX IF NOT EXISTS idx_message_sticker_message_id
    ON message_sticker (message_id);

-- models/message_forward.rs  →  message_forward
CREATE TABLE IF NOT EXISTS message_forward (
    id            UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    message_id          UUID NOT NULL REFERENCES message(id) ON DELETE CASCADE,
    source_message_id   UUID NOT NULL REFERENCES message(id) ON DELETE CASCADE,
    source_channel_id   UUID NOT NULL,
    forwarded_by        UUID NOT NULL,
    created_at          TIMESTAMPTZ NOT NULL DEFAULT now()
);

CREATE INDEX IF NOT EXISTS idx_message_forward_message_id
    ON message_forward (message_id);
CREATE INDEX IF NOT EXISTS idx_message_forward_source_message_id
    ON message_forward (source_message_id);

-- models/message_component.rs  →  message_component
CREATE TABLE IF NOT EXISTS message_component (
    id            UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    message_id      UUID NOT NULL REFERENCES message(id) ON DELETE CASCADE,
    row             INTEGER NOT NULL DEFAULT 0,
    position        INTEGER NOT NULL DEFAULT 0,
    component_type  TEXT NOT NULL,
    custom_id       TEXT NOT NULL,
    label           TEXT NULL,
    emoji           TEXT NULL,
    style           TEXT NULL,
    url             TEXT NULL,
    disabled        BOOLEAN NOT NULL DEFAULT FALSE,
    placeholder     TEXT NULL,
    min_values      INTEGER NULL,
    max_values      INTEGER NULL,
    options         JSONB NULL,
    created_at      TIMESTAMPTZ NOT NULL DEFAULT now()
);

CREATE INDEX IF NOT EXISTS idx_message_component_message_id
    ON message_component (message_id);

COMMIT;
