
    create table arcs (
        "order" integer not null,
        campaign_id bigint not null,
        created_at timestamp(6) not null,
        id bigint generated by default as identity,
        updated_at timestamp(6) not null,
        type varchar(16) default 'LINEAR' not null check (type in ('LINEAR','HUB')),
        description TEXT,
        gm_notes TEXT,
        icon varchar(255),
        illustration_image_ids TEXT,
        map_image_ids TEXT,
        name varchar(255) not null,
        related_page_ids TEXT,
        resolution TEXT,
        rewards TEXT,
        stakes TEXT,
        themes TEXT,
        primary key (id)
    );

    create table campaigns (
        arcs_count integer not null,
        created_at timestamp(6) not null,
        id bigint generated by default as identity,
        updated_at timestamp(6) not null,
        description TEXT,
        game_system_id varchar(255),
        lore_id varchar(255),
        name varchar(255) not null,
        primary key (id)
    );

    create table catalog_items (
        position integer not null,
        catalog_id bigint not null,
        id bigint generated by default as identity,
        price varchar(64),
        category varchar(128),
        description TEXT,
        name varchar(255) not null,
        primary key (id)
    );

    create table chapters (
        "order" integer not null,
        arc_id bigint not null,
        created_at timestamp(6) not null,
        id bigint generated by default as identity,
        updated_at timestamp(6) not null,
        description TEXT,
        gm_notes TEXT,
        icon varchar(255),
        illustration_image_ids TEXT,
        map_image_ids TEXT,
        name varchar(255) not null,
        narrative_stakes TEXT,
        player_objectives TEXT,
        prerequisites TEXT,
        related_page_ids TEXT,
        primary key (id)
    );

    create table characters (
        "order" integer not null,
        campaign_id bigint,
        created_at timestamp(6) not null,
        id bigint generated by default as identity,
        playthrough_id bigint,
        updated_at timestamp(6) not null,
        field_values TEXT,
        header_image_id varchar(255),
        image_values TEXT,
        key_value_values TEXT,
        name varchar(255) not null,
        portrait_image_id varchar(255),
        primary key (id)
    );

    create table conversation_messages (
        conversation_id bigint not null,
        created_at timestamp(6) not null,
        id bigint generated by default as identity,
        role varchar(16) not null,
        content TEXT not null,
        primary key (id)
    );

    create table conversations (
        created_at timestamp(6) not null,
        id bigint generated by default as identity,
        updated_at timestamp(6) not null,
        campaign_id varchar(255),
        entity_id varchar(255),
        entity_type varchar(255),
        lore_id varchar(255),
        title varchar(255) not null,
        primary key (id)
    );

    create table enemies (
        "order" integer not null,
        campaign_id bigint not null,
        created_at timestamp(6) not null,
        id bigint generated by default as identity,
        updated_at timestamp(6) not null,
        field_values TEXT,
        folder varchar(255),
        header_image_id varchar(255),
        image_values TEXT,
        key_value_values TEXT,
        level varchar(255),
        name varchar(255) not null,
        portrait_image_id varchar(255),
        primary key (id)
    );

    create table game_systems (
        is_public boolean not null,
        created_at timestamp(6) not null,
        id bigint generated by default as identity,
        updated_at timestamp(6) not null,
        author varchar(255),
        character_template TEXT,
        description TEXT,
        enemy_template TEXT,
        name varchar(255) not null,
        npc_template TEXT,
        rules_markdown TEXT,
        primary key (id)
    );

    create table images (
        id bigint generated by default as identity,
        size_bytes bigint not null,
        uploaded_at timestamp(6) not null,
        content_type varchar(255) not null,
        filename varchar(255) not null,
        storage_key varchar(255) not null unique,
        primary key (id)
    );

    create table item_catalogs (
        "order" integer not null,
        campaign_id bigint not null,
        created_at timestamp(6) not null,
        id bigint generated by default as identity,
        updated_at timestamp(6) not null,
        icon varchar(64),
        description TEXT,
        name varchar(255) not null,
        primary key (id)
    );

    create table licenses (
        beta_channel_enabled boolean not null,
        last_refresh_succeeded boolean not null,
        created_at timestamp(6) with time zone not null,
        expires_at timestamp(6) with time zone not null,
        issued_at timestamp(6) with time zone not null,
        last_refresh_attempt_at timestamp(6) with time zone,
        updated_at timestamp(6) with time zone not null,
        id varchar(255) not null,
        instance_id varchar(255) not null,
        patreon_user_id varchar(255) not null,
        raw_jwt TEXT not null,
        tier_id varchar(255) not null,
        primary key (id)
    );

    create table lore_nodes (
        created_at timestamp(6) not null,
        id bigint generated by default as identity,
        lore_id bigint not null,
        parent_id bigint,
        updated_at timestamp(6) not null,
        icon varchar(64),
        name varchar(255) not null,
        primary key (id)
    );

    create table lores (
        node_count integer not null,
        page_count integer not null,
        created_at timestamp(6) not null,
        id bigint generated by default as identity,
        updated_at timestamp(6) not null,
        description TEXT,
        name varchar(255) not null,
        primary key (id)
    );

    create table notebook_messages (
        archived_at timestamp(6),
        created_at timestamp(6) not null,
        id bigint generated by default as identity,
        notebook_id bigint not null,
        role varchar(16) not null,
        content TEXT not null,
        primary key (id)
    );

    create table notebook_sources (
        chunk_count integer not null,
        page_count integer not null,
        created_at timestamp(6) not null,
        id bigint generated by default as identity,
        notebook_id bigint not null,
        status varchar(16) not null,
        filename varchar(255) not null,
        primary key (id)
    );

    create table notebooks (
        campaign_id bigint not null,
        created_at timestamp(6) not null,
        id bigint generated by default as identity,
        updated_at timestamp(6) not null,
        name varchar(255) not null,
        primary key (id)
    );

    create table npcs (
        "order" integer not null,
        campaign_id bigint not null,
        created_at timestamp(6) not null,
        id bigint generated by default as identity,
        updated_at timestamp(6) not null,
        field_values TEXT,
        folder varchar(255),
        header_image_id varchar(255),
        image_values TEXT,
        key_value_values TEXT,
        name varchar(255) not null,
        portrait_image_id varchar(255),
        related_page_ids TEXT,
        primary key (id)
    );

    create table pages (
        created_at timestamp(6) not null,
        id bigint generated by default as identity,
        lore_id bigint not null,
        node_id bigint not null,
        template_id bigint,
        updated_at timestamp(6) not null,
        image_values_json TEXT,
        key_value_values TEXT,
        notes TEXT,
        related_page_ids TEXT,
        table_values TEXT,
        tags TEXT,
        title varchar(255) not null,
        values_json TEXT,
        primary key (id)
    );

    create table playthrough_flag (
        value boolean not null,
        id bigint generated by default as identity,
        playthrough_id bigint not null,
        name varchar(128) not null,
        primary key (id),
        constraint uk_playthrough_flag_name unique (playthrough_id, name)
    );

    create table playthroughs (
        campaign_id bigint not null,
        created_at timestamp(6) not null,
        id bigint generated by default as identity,
        updated_at timestamp(6) not null,
        description TEXT,
        name varchar(255) not null,
        primary key (id)
    );

    create table quest_progression (
        chapter_id bigint not null,
        id bigint generated by default as identity,
        playthrough_id bigint not null,
        status varchar(16) not null check (status in ('NOT_STARTED','IN_PROGRESS','COMPLETED')),
        primary key (id),
        constraint uk_quest_progression_unique unique (playthrough_id, chapter_id)
    );

    create table random_table_entries (
        max_roll integer not null,
        min_roll integer not null,
        position integer not null,
        id bigint generated by default as identity,
        random_table_id bigint not null,
        detail TEXT,
        label varchar(255) not null,
        primary key (id)
    );

    create table random_tables (
        "order" integer not null,
        campaign_id bigint not null,
        created_at timestamp(6) not null,
        id bigint generated by default as identity,
        updated_at timestamp(6) not null,
        dice_formula varchar(32) not null,
        icon varchar(64),
        description TEXT,
        name varchar(255) not null,
        primary key (id)
    );

    create table scenes (
        "order" integer not null,
        chapter_id bigint not null,
        created_at timestamp(6) not null,
        id bigint generated by default as identity,
        updated_at timestamp(6) not null,
        atmosphere TEXT,
        branches TEXT,
        choices_consequences TEXT,
        combat_difficulty TEXT,
        description TEXT,
        enemies TEXT,
        enemy_ids TEXT,
        gm_secret_notes TEXT,
        icon varchar(255),
        illustration_image_ids TEXT,
        location TEXT,
        map_image_ids TEXT,
        name varchar(255) not null,
        player_narration TEXT,
        related_page_ids TEXT,
        rooms TEXT,
        timing TEXT,
        primary key (id)
    );

    create table session_entries (
        created_at timestamp(6) not null,
        id bigint generated by default as identity,
        occurred_at timestamp(6) not null,
        updated_at timestamp(6) not null,
        type varchar(32) not null check (type in ('NOTE','EVENT','DICE_ROLL','PLAYER_ACTION')),
        content TEXT not null,
        session_id varchar(255) not null,
        primary key (id)
    );

    create table sessions (
        created_at timestamp(6) not null,
        ended_at timestamp(6),
        id bigint generated by default as identity,
        playthrough_id bigint,
        started_at timestamp(6) not null,
        updated_at timestamp(6) not null,
        campaign_id varchar(255),
        name varchar(255) not null,
        primary key (id)
    );

    create table templates (
        created_at timestamp(6) not null,
        default_node_id bigint,
        id bigint generated by default as identity,
        lore_id bigint not null,
        updated_at timestamp(6) not null,
        description TEXT,
        fields TEXT,
        name varchar(255) not null,
        primary key (id)
    );

    create index idx_catalog_items_catalog_id 
       on catalog_items (catalog_id);

    create index idx_conv_lore_entity 
       on conversations (lore_id, entity_type, entity_id, updated_at);

    create index idx_conv_campaign_entity 
       on conversations (campaign_id, entity_type, entity_id, updated_at);

    create index idx_enemies_campaign_id 
       on enemies (campaign_id);

    create index idx_item_catalogs_campaign_id 
       on item_catalogs (campaign_id);

    create index idx_notebook_messages_notebook_id 
       on notebook_messages (notebook_id);

    create index idx_notebook_sources_notebook_id 
       on notebook_sources (notebook_id);

    create index idx_notebooks_campaign_id 
       on notebooks (campaign_id);

    create index ix_playthrough_flag_playthrough 
       on playthrough_flag (playthrough_id);

    create index ix_playthrough_campaign 
       on playthroughs (campaign_id);

    create index ix_quest_progression_playthrough 
       on quest_progression (playthrough_id);

    create index idx_random_table_entries_table_id 
       on random_table_entries (random_table_id);

    create index idx_session_entries_session_id 
       on session_entries (session_id);

    alter table if exists catalog_items 
       add constraint FK7tuoq6mwuc00yv0hhpiw4aoni 
       foreign key (catalog_id) 
       references item_catalogs;

    alter table if exists conversation_messages 
       add constraint FKcr8qqgnqnaqq2hw3gr4wtfe2a 
       foreign key (conversation_id) 
       references conversations;

    alter table if exists random_table_entries 
       add constraint FKly4syvpfit2kibfjut67xxrrq 
       foreign key (random_table_id) 
       references random_tables;
