-- Migration 021: PeerTube integration
-- Creates the peertube_settings singleton table and the peertube_upload_enabled feature flag.
-- The upload flag defaults to 0 (disabled) so existing deployments are unaffected.

CREATE TABLE IF NOT EXISTS peertube_settings (
    id           INTEGER PRIMARY KEY CHECK (id = 1),  -- singleton row
    instance_url TEXT    NOT NULL DEFAULT '',
    username     TEXT    NOT NULL DEFAULT '',
    password     TEXT    NOT NULL DEFAULT '',  -- AES-256-GCM encrypted via Crypto.php
    channel_id   INTEGER NOT NULL DEFAULT 1,
    privacy      INTEGER NOT NULL DEFAULT 1,   -- 1=Public 2=Unlisted 3=Private
    updated_at   TEXT    NOT NULL DEFAULT CURRENT_TIMESTAMP
);

-- Insert the singleton placeholder row so UPDATE always finds it
INSERT OR IGNORE INTO peertube_settings (id) VALUES (1);

-- Feature flag: disabled by default (waiting for upload quota)
INSERT INTO site_settings (key, value, updated_at)
VALUES ('peertube_upload_enabled', '0', CURRENT_TIMESTAMP)
ON CONFLICT(key) DO NOTHING;
