-- Dendrite test schema + seed data
-- This creates the minimum Dendrite tables needed for migration testing

-- === Sequences ===
CREATE SEQUENCE IF NOT EXISTS roomserver_room_nid_seq;
CREATE SEQUENCE IF NOT EXISTS roomserver_event_nid_seq;
CREATE SEQUENCE IF NOT EXISTS roomserver_event_type_nid_seq START 65536;
CREATE SEQUENCE IF NOT EXISTS roomserver_event_state_key_nid_seq START 65536;
CREATE SEQUENCE IF NOT EXISTS roomserver_state_snapshot_nid_seq;
CREATE SEQUENCE IF NOT EXISTS roomserver_state_block_nid_seq;
CREATE SEQUENCE IF NOT EXISTS syncapi_stream_id;
CREATE SEQUENCE IF NOT EXISTS syncapi_receipt_id;

-- === User tables ===
CREATE TABLE IF NOT EXISTS userapi_accounts (
    localpart TEXT NOT NULL,
    server_name TEXT NOT NULL,
    created_ts BIGINT NOT NULL,
    password_hash TEXT,
    appservice_id TEXT,
    is_deactivated BOOLEAN DEFAULT FALSE,
    account_type SMALLINT NOT NULL,
    UNIQUE (localpart, server_name)
);

CREATE TABLE IF NOT EXISTS userapi_profiles (
    localpart TEXT NOT NULL,
    server_name TEXT NOT NULL,
    display_name TEXT,
    avatar_url TEXT,
    UNIQUE (localpart, server_name)
);

CREATE TABLE IF NOT EXISTS userapi_devices (
    access_token TEXT NOT NULL PRIMARY KEY,
    device_id TEXT NOT NULL,
    localpart TEXT NOT NULL,
    server_name TEXT NOT NULL,
    created_ts BIGINT NOT NULL,
    display_name TEXT,
    last_seen_ts BIGINT NOT NULL,
    ip TEXT,
    user_agent TEXT,
    UNIQUE (localpart, server_name, device_id)
);

-- === Room tables ===
CREATE TABLE IF NOT EXISTS roomserver_rooms (
    room_nid BIGINT PRIMARY KEY DEFAULT nextval('roomserver_room_nid_seq'),
    room_id TEXT NOT NULL UNIQUE,
    latest_event_nids BIGINT[] NOT NULL DEFAULT '{}'::BIGINT[],
    last_event_sent_nid BIGINT NOT NULL DEFAULT 0,
    state_snapshot_nid BIGINT NOT NULL DEFAULT 0,
    room_version TEXT NOT NULL
);

CREATE TABLE IF NOT EXISTS roomserver_room_aliases (
    alias TEXT NOT NULL PRIMARY KEY,
    room_id TEXT NOT NULL,
    creator_id TEXT NOT NULL
);

-- === Event type/state key lookup tables ===
CREATE TABLE IF NOT EXISTS roomserver_event_types (
    event_type_nid BIGINT PRIMARY KEY DEFAULT nextval('roomserver_event_type_nid_seq'),
    event_type TEXT NOT NULL UNIQUE
);

-- Preassigned event types
INSERT INTO roomserver_event_types (event_type_nid, event_type) VALUES
    (1, 'm.room.create'),
    (2, 'm.room.power_levels'),
    (3, 'm.room.join_rules'),
    (4, 'm.room.third_party_invite'),
    (5, 'm.room.member'),
    (6, 'm.room.redaction'),
    (7, 'm.room.history_visibility')
ON CONFLICT DO NOTHING;

CREATE TABLE IF NOT EXISTS roomserver_event_state_keys (
    event_state_key_nid BIGINT PRIMARY KEY DEFAULT nextval('roomserver_event_state_key_nid_seq'),
    event_state_key TEXT NOT NULL UNIQUE
);

-- Preassigned: nid 1 = empty string
INSERT INTO roomserver_event_state_keys (event_state_key_nid, event_state_key) VALUES (1, '')
ON CONFLICT DO NOTHING;

-- === Event tables ===
CREATE TABLE IF NOT EXISTS roomserver_events (
    event_nid BIGINT PRIMARY KEY DEFAULT nextval('roomserver_event_nid_seq'),
    room_nid BIGINT NOT NULL,
    event_type_nid BIGINT NOT NULL,
    event_state_key_nid BIGINT NOT NULL,
    sent_to_output BOOLEAN NOT NULL DEFAULT FALSE,
    state_snapshot_nid BIGINT NOT NULL DEFAULT 0,
    depth BIGINT NOT NULL,
    event_id TEXT NOT NULL UNIQUE,
    auth_event_nids BIGINT[] NOT NULL,
    is_rejected BOOLEAN NOT NULL DEFAULT FALSE
);

CREATE TABLE IF NOT EXISTS roomserver_event_json (
    event_nid BIGINT NOT NULL PRIMARY KEY,
    event_json TEXT NOT NULL
);

-- === Membership ===
CREATE TABLE IF NOT EXISTS roomserver_membership (
    room_nid BIGINT NOT NULL,
    target_nid BIGINT NOT NULL,
    sender_nid BIGINT NOT NULL DEFAULT 0,
    membership_nid BIGINT NOT NULL DEFAULT 1,
    event_nid BIGINT NOT NULL DEFAULT 0,
    target_local BOOLEAN NOT NULL DEFAULT FALSE,
    forgotten BOOLEAN NOT NULL DEFAULT FALSE,
    UNIQUE (room_nid, target_nid)
);

-- === Sync API tables ===
CREATE TABLE IF NOT EXISTS syncapi_current_room_state (
    room_id TEXT NOT NULL,
    event_id TEXT NOT NULL,
    type TEXT NOT NULL,
    sender TEXT NOT NULL,
    contains_url BOOL NOT NULL,
    state_key TEXT NOT NULL,
    headered_event_json TEXT NOT NULL,
    membership TEXT,
    added_at BIGINT,
    history_visibility SMALLINT NOT NULL DEFAULT 2,
    UNIQUE (room_id, type, state_key)
);

CREATE TABLE IF NOT EXISTS syncapi_receipts (
    id BIGINT PRIMARY KEY DEFAULT nextval('syncapi_receipt_id'),
    room_id TEXT NOT NULL,
    receipt_type TEXT NOT NULL,
    user_id TEXT NOT NULL,
    event_id TEXT NOT NULL,
    receipt_ts BIGINT NOT NULL,
    UNIQUE (room_id, receipt_type, user_id)
);

-- === Redactions ===
CREATE TABLE IF NOT EXISTS roomserver_redactions (
    redaction_event_id TEXT PRIMARY KEY,
    redacts_event_id TEXT NOT NULL,
    validated BOOLEAN NOT NULL
);

-- === Media ===
CREATE TABLE IF NOT EXISTS mediaapi_media_repository (
    media_id TEXT NOT NULL,
    media_origin TEXT NOT NULL,
    content_type TEXT NOT NULL,
    file_size_bytes BIGINT NOT NULL,
    creation_ts BIGINT NOT NULL,
    upload_name TEXT NOT NULL,
    base64hash TEXT NOT NULL,
    user_id TEXT NOT NULL,
    UNIQUE (media_id, media_origin)
);

CREATE TABLE IF NOT EXISTS mediaapi_thumbnail (
    media_id TEXT NOT NULL,
    media_origin TEXT NOT NULL,
    content_type TEXT NOT NULL,
    file_size_bytes BIGINT NOT NULL,
    creation_ts BIGINT NOT NULL,
    width INTEGER NOT NULL,
    height INTEGER NOT NULL,
    resize_method TEXT NOT NULL,
    UNIQUE (media_id, media_origin, width, height, resize_method)
);

-- =============================================
-- SEED TEST DATA
-- =============================================

-- Users
INSERT INTO userapi_accounts (localpart, server_name, created_ts, password_hash, account_type)
VALUES
    ('alice', 'test.local', 1700000000000, '$2a$12$fakehash_alice', 1),
    ('bob', 'test.local', 1700000100000, '$2a$12$fakehash_bob', 1),
    ('admin', 'test.local', 1699999000000, '$2a$12$fakehash_admin', 3);

INSERT INTO userapi_profiles (localpart, server_name, display_name, avatar_url)
VALUES
    ('alice', 'test.local', 'Alice Wonderland', 'mxc://test.local/alice_avatar'),
    ('bob', 'test.local', 'Bob Builder', NULL),
    ('admin', 'test.local', 'Admin', NULL);

INSERT INTO userapi_devices (access_token, device_id, localpart, server_name, created_ts, display_name, last_seen_ts, ip, user_agent)
VALUES
    ('token_alice_1', 'DEVICE_A1', 'alice', 'test.local', 1700000000000, 'Alice Phone', 1700100000000, '192.168.1.10', 'Element/1.0'),
    ('token_bob_1', 'DEVICE_B1', 'bob', 'test.local', 1700000100000, 'Bob Laptop', 1700100000000, '192.168.1.11', 'Element/1.0');

-- Rooms
INSERT INTO roomserver_rooms (room_nid, room_id, room_version) VALUES
    (1, '!room1:test.local', '10'),
    (2, '!room2:test.local', '10');

-- Additional event type for m.room.message
INSERT INTO roomserver_event_types (event_type_nid, event_type) VALUES (65536, 'm.room.message') ON CONFLICT DO NOTHING;
-- Event type for m.room.name
INSERT INTO roomserver_event_types (event_type_nid, event_type) VALUES (65537, 'm.room.name') ON CONFLICT DO NOTHING;

-- State keys for users
INSERT INTO roomserver_event_state_keys (event_state_key_nid, event_state_key) VALUES
    (65536, '@alice:test.local'),
    (65537, '@bob:test.local')
ON CONFLICT DO NOTHING;

-- Room aliases
INSERT INTO roomserver_room_aliases (alias, room_id, creator_id) VALUES
    ('#general:test.local', '!room1:test.local', '@alice:test.local');

-- === Events for Room 1 ===

-- m.room.create event
INSERT INTO roomserver_events (event_nid, room_nid, event_type_nid, event_state_key_nid, depth, event_id, auth_event_nids) VALUES
    (1, 1, 1, 1, 1, '$create_room1', '{}');
INSERT INTO roomserver_event_json (event_nid, event_json) VALUES
    (1, '{"event_id":"$create_room1","room_id":"!room1:test.local","type":"m.room.create","sender":"@alice:test.local","state_key":"","origin_server_ts":1700000001000,"content":{"creator":"@alice:test.local","room_version":"10"},"auth_events":[],"prev_events":[]}');

-- m.room.member join alice
INSERT INTO roomserver_events (event_nid, room_nid, event_type_nid, event_state_key_nid, depth, event_id, auth_event_nids) VALUES
    (2, 1, 5, 65536, 2, '$join_alice_room1', '{1}');
INSERT INTO roomserver_event_json (event_nid, event_json) VALUES
    (2, '{"event_id":"$join_alice_room1","room_id":"!room1:test.local","type":"m.room.member","sender":"@alice:test.local","state_key":"@alice:test.local","origin_server_ts":1700000002000,"content":{"membership":"join","displayname":"Alice Wonderland"},"auth_events":["$create_room1"],"prev_events":["$create_room1"]}');

-- m.room.member join bob
INSERT INTO roomserver_events (event_nid, room_nid, event_type_nid, event_state_key_nid, depth, event_id, auth_event_nids) VALUES
    (3, 1, 5, 65537, 3, '$join_bob_room1', '{1,2}');
INSERT INTO roomserver_event_json (event_nid, event_json) VALUES
    (3, '{"event_id":"$join_bob_room1","room_id":"!room1:test.local","type":"m.room.member","sender":"@bob:test.local","state_key":"@bob:test.local","origin_server_ts":1700000003000,"content":{"membership":"join","displayname":"Bob Builder"},"auth_events":["$create_room1","$join_alice_room1"],"prev_events":["$join_alice_room1"]}');

-- m.room.name
INSERT INTO roomserver_events (event_nid, room_nid, event_type_nid, event_state_key_nid, depth, event_id, auth_event_nids) VALUES
    (4, 1, 65537, 1, 4, '$name_room1', '{1,2}');
INSERT INTO roomserver_event_json (event_nid, event_json) VALUES
    (4, '{"event_id":"$name_room1","room_id":"!room1:test.local","type":"m.room.name","sender":"@alice:test.local","state_key":"","origin_server_ts":1700000004000,"content":{"name":"General Chat"},"auth_events":["$create_room1","$join_alice_room1"],"prev_events":["$join_bob_room1"]}');

-- m.room.message from alice
INSERT INTO roomserver_events (event_nid, room_nid, event_type_nid, event_state_key_nid, depth, event_id, auth_event_nids) VALUES
    (5, 1, 65536, 0, 5, '$msg1_room1', '{1,2}');
INSERT INTO roomserver_event_json (event_nid, event_json) VALUES
    (5, '{"event_id":"$msg1_room1","room_id":"!room1:test.local","type":"m.room.message","sender":"@alice:test.local","origin_server_ts":1700000010000,"content":{"msgtype":"m.text","body":"Hello everyone!"},"auth_events":["$create_room1","$join_alice_room1"],"prev_events":["$name_room1"]}');

-- m.room.message from bob
INSERT INTO roomserver_events (event_nid, room_nid, event_type_nid, event_state_key_nid, depth, event_id, auth_event_nids) VALUES
    (6, 1, 65536, 0, 6, '$msg2_room1', '{1,3}');
INSERT INTO roomserver_event_json (event_nid, event_json) VALUES
    (6, '{"event_id":"$msg2_room1","room_id":"!room1:test.local","type":"m.room.message","sender":"@bob:test.local","origin_server_ts":1700000020000,"content":{"msgtype":"m.text","body":"Hi Alice!"},"auth_events":["$create_room1","$join_bob_room1"],"prev_events":["$msg1_room1"]}');

-- m.room.message with URL
INSERT INTO roomserver_events (event_nid, room_nid, event_type_nid, event_state_key_nid, depth, event_id, auth_event_nids) VALUES
    (7, 1, 65536, 0, 7, '$msg3_room1', '{1,2}');
INSERT INTO roomserver_event_json (event_nid, event_json) VALUES
    (7, '{"event_id":"$msg3_room1","room_id":"!room1:test.local","type":"m.room.message","sender":"@alice:test.local","origin_server_ts":1700000030000,"content":{"msgtype":"m.image","body":"photo.jpg","url":"mxc://test.local/media123"},"auth_events":["$create_room1","$join_alice_room1"],"prev_events":["$msg2_room1"]}');

-- === Events for Room 2 (small DM) ===
INSERT INTO roomserver_events (event_nid, room_nid, event_type_nid, event_state_key_nid, depth, event_id, auth_event_nids) VALUES
    (8, 2, 1, 1, 1, '$create_room2', '{}');
INSERT INTO roomserver_event_json (event_nid, event_json) VALUES
    (8, '{"event_id":"$create_room2","room_id":"!room2:test.local","type":"m.room.create","sender":"@bob:test.local","state_key":"","origin_server_ts":1700001000000,"content":{"creator":"@bob:test.local","room_version":"10"},"auth_events":[],"prev_events":[]}');

INSERT INTO roomserver_events (event_nid, room_nid, event_type_nid, event_state_key_nid, depth, event_id, auth_event_nids) VALUES
    (9, 2, 5, 65537, 2, '$join_bob_room2', '{8}');
INSERT INTO roomserver_event_json (event_nid, event_json) VALUES
    (9, '{"event_id":"$join_bob_room2","room_id":"!room2:test.local","type":"m.room.member","sender":"@bob:test.local","state_key":"@bob:test.local","origin_server_ts":1700001001000,"content":{"membership":"join","displayname":"Bob Builder"},"auth_events":["$create_room2"],"prev_events":["$create_room2"]}');

-- A rejected event (should be skipped)
INSERT INTO roomserver_events (event_nid, room_nid, event_type_nid, event_state_key_nid, depth, event_id, auth_event_nids, is_rejected) VALUES
    (10, 1, 65536, 0, 8, '$rejected_event', '{1}', TRUE);
INSERT INTO roomserver_event_json (event_nid, event_json) VALUES
    (10, '{"event_id":"$rejected_event","room_id":"!room1:test.local","type":"m.room.message","sender":"@evil:other.server","origin_server_ts":1700000050000,"content":{"msgtype":"m.text","body":"spam"},"auth_events":["$create_room1"],"prev_events":["$msg3_room1"]}');

-- === Syncapi current room state ===
INSERT INTO syncapi_current_room_state (room_id, event_id, type, sender, contains_url, state_key, headered_event_json, membership) VALUES
    ('!room1:test.local', '$create_room1', 'm.room.create', '@alice:test.local', false, '', '{"event_id":"$create_room1","room_id":"!room1:test.local","type":"m.room.create","sender":"@alice:test.local","state_key":"","content":{"creator":"@alice:test.local","room_version":"10"}}', NULL),
    ('!room1:test.local', '$join_alice_room1', 'm.room.member', '@alice:test.local', false, '@alice:test.local', '{"event_id":"$join_alice_room1","room_id":"!room1:test.local","type":"m.room.member","sender":"@alice:test.local","state_key":"@alice:test.local","content":{"membership":"join","displayname":"Alice Wonderland"}}', 'join'),
    ('!room1:test.local', '$join_bob_room1', 'm.room.member', '@bob:test.local', false, '@bob:test.local', '{"event_id":"$join_bob_room1","room_id":"!room1:test.local","type":"m.room.member","sender":"@bob:test.local","state_key":"@bob:test.local","content":{"membership":"join","displayname":"Bob Builder"}}', 'join'),
    ('!room1:test.local', '$name_room1', 'm.room.name', '@alice:test.local', false, '', '{"event_id":"$name_room1","room_id":"!room1:test.local","type":"m.room.name","sender":"@alice:test.local","state_key":"","content":{"name":"General Chat"}}', NULL),
    ('!room2:test.local', '$create_room2', 'm.room.create', '@bob:test.local', false, '', '{"event_id":"$create_room2","room_id":"!room2:test.local","type":"m.room.create","sender":"@bob:test.local","state_key":"","content":{"creator":"@bob:test.local","room_version":"10"}}', NULL),
    ('!room2:test.local', '$join_bob_room2', 'm.room.member', '@bob:test.local', false, '@bob:test.local', '{"event_id":"$join_bob_room2","room_id":"!room2:test.local","type":"m.room.member","sender":"@bob:test.local","state_key":"@bob:test.local","content":{"membership":"join","displayname":"Bob Builder"}}', 'join');

-- === Receipts ===
INSERT INTO syncapi_receipts (room_id, receipt_type, user_id, event_id, receipt_ts) VALUES
    ('!room1:test.local', 'm.read', '@alice:test.local', '$msg2_room1', 1700000025000),
    ('!room1:test.local', 'm.read', '@bob:test.local', '$msg3_room1', 1700000035000);

-- === Redactions ===
-- (none in test data, but table exists)

-- === Media ===
INSERT INTO mediaapi_media_repository (media_id, media_origin, content_type, file_size_bytes, creation_ts, upload_name, base64hash, user_id) VALUES
    ('media123', 'test.local', 'image/jpeg', 12345, 1700000030000, 'photo.jpg', 'abc123def456ghi789', '@alice:test.local'),
    ('media456', 'test.local', 'application/pdf', 98765, 1700000040000, 'document.pdf', 'xyz789abc123def456', '@bob:test.local');

INSERT INTO mediaapi_thumbnail (media_id, media_origin, content_type, file_size_bytes, creation_ts, width, height, resize_method) VALUES
    ('media123', 'test.local', 'image/jpeg', 3000, 1700000030000, 320, 240, 'scale');
