CREATE TABLE IF NOT EXISTS setup (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    
    is_admin BOOLEAN NOT NULL DEFAULT FALSE,
    is_setup BOOLEAN NOT NULL DEFAULT TRUE,

    created_at DATETIME NOT NULL DEFAULT current_timestamp
);

CREATE TABLE IF NOT EXISTS game (
    id INTEGER PRIMARY KEY AUTOINCREMENT
    
    demo_mode BOOLEAN NOT NULL DEFAULT FALSE,
    idle_logout BOOLEAN NOT NULL DEFAULT FALSE,
    paused BOOLEAN NOT NULL DEFAULT FALSE,
    user_id VARCHAR(128) NOT NULL,

    created_at DATETIME NOT NULL DEFAULT current_timestamp
);

CREATE TABLE IF NOT EXISTS world (
    id TEXT PRIMARY KEY,
    
    socket BOOLEAN NOT NULL DEFAULT FALSE,
    protected BOOLEAN NOT NULL DEFAULT FALSE,
    exclusive_ BOOLEAN NOT NULL DEFAULT FALSE,
    persistent_storage BOOLEAN NOT NULL DEFAULT FALSE,
    locked BOOLEAN NOT NULL DEFAULT FALSE,
    owned BOOLEAN NOT NULL DEFAULT FALSE,
    has_storage BOOLEAN NOT NULL DEFAULT FALSE,
    playtime INTEGER NOT NULL,
    availability INTEGER NOT NULL,
    next_session DATETIME NOT NULL,

    title VARCHAR(128) NOT NULL,
    description VARCHAR(128) NOT NULL,
    version VARCHAR(128) NOT NULL,
    system VARCHAR(128) NOT NULL,
    background VARCHAR(128) NOT NULL,
    join_theme VARCHAR(128) NOT NULL,
    core_version VARCHAR(128) NOT NULL,
    system_version VARCHAR(128) NOT NULL,
    last_played VARCHAR(128) NOT NULL,

    setup_id INTEGER,
    FOREIGN KEY (setup_id) REFERENCES setup(id) ON DELETE CASCADE
    game_id INTEGER UNIQUE,
    FOREIGN KEY (game_id) REFERENCES game(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS system (
    id TEXT PRIMARY KEY,
    
    title VARCHAR(128) NOT NULL,
    description VARCHAR(128) NOT NULL,
    url VARCHAR(128) NOT NULL,
    license VARCHAR(128) NOT NULL,
    bugs VARCHAR(128) NOT NULL,
    changelog VARCHAR(128) NOT NULL,
    version VARCHAR(128) NOT NULL,
    socket BOOLEAN NOT NULL DEFAULT FALSE,
    manifest VARCHAR(128) NOT NULL,
    download VARCHAR(128) NOT NULL,
    protected BOOLEAN NOT NULL DEFAULT FALSE,
    exclusive_ BOOLEAN NOT NULL DEFAULT FALSE,
    persistent_storage BOOLEAN NOT NULL DEFAULT FALSE,
    background VARCHAR(128) NOT NULL DEFAULT FALSE,
    primary_token_attribute VARCHAR(128) NOT NULL DEFAULT FALSE,
    availability INTEGER NOT NULL DEFAULT FALSE,
    locked BOOLEAN NOT NULL DEFAULT FALSE,
    owned BOOLEAN NOT NULL DEFAULT FALSE,
    has_storage BOOLEAN NOT NULL DEFAULT FALSE,

    setup_id INTEGER,
    FOREIGN KEY (setup_id) REFERENCES setup(id) ON DELETE CASCADE
    game_id INTEGER UNIQUE,
    FOREIGN KEY (game_id) REFERENCES game(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS module (
    id TEXT PRIMARY KEY,
    
    title VARCHAR(128) NOT NULL,
    description VARCHAR(128) NOT NULL,
    url VARCHAR(128) NOT NULL,
    license VARCHAR(128) NOT NULL,
    readme VARCHAR(128) NOT NULL,
    bugs VARCHAR(128) NOT NULL,
    changelog VARCHAR(128) NOT NULL,
    version VARCHAR(128) NOT NULL,
    socket BOOLEAN NOT NULL DEFAULT FALSE,
    manifest VARCHAR(128) NOT NULL,
    download VARCHAR(128) NOT NULL,
    protected BOOLEAN NOT NULL DEFAULT FALSE,
    exclusive_ BOOLEAN NOT NULL DEFAULT FALSE,
    persistent_storage BOOLEAN NOT NULL DEFAULT FALSE,
    core_translation BOOLEAN NOT NULL DEFAULT FALSE,
    library BOOLEAN NOT NULL DEFAULT FALSE,
    availability INTEGER NOT NULL DEFAULT FALSE,
    locked BOOLEAN NOT NULL DEFAULT FALSE,
    owned BOOLEAN NOT NULL DEFAULT FALSE,
    has_storage BOOLEAN NOT NULL DEFAULT FALSE,
    active BOOLEAN NOT NULL DEFAULT FALSE,

    setup_id INTEGER,
    FOREIGN KEY (setup_id) REFERENCES setup(id) ON DELETE CASCADE
    game_id INTEGER,
    FOREIGN KEY (game_id) REFERENCES game(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS addresses (
    id INTEGER PRIMARY KEY AUTOINCREMENT,

    local VARCHAR(128) NOT NULL,
    remote VARCHAR(128) NOT NULL,
    remote_is_accessible BOOLEAN NOT NULL DEFAULT FALSE,

    game_id INTEGER UNIQUE,
    FOREIGN KEY (game_id) REFERENCES game(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS setup_options (
    id INTEGER PRIMARY KEY AUTOINCREMENT,

    compress_socket BOOLEAN NOT NULL DEFAULT FALSE,
    compress_static BOOLEAN NOT NULL DEFAULT FALSE,
    css_theme VARCHAR(128) NOT NULL,
    data_path VARCHAR(128) NOT NULL,
    fullscreen BOOLEAN NOT NULL DEFAULT FALSE,
    hostname VARCHAR(128) NOT NULL,
    hot_reload BOOLEAN NOT NULL DEFAULT FALSE,
    language VARCHAR(128) NOT NULL,
    local_hostname VARCHAR(128) NOT NULL,
    port INTEGER NOT NULL,
    proxy_ssl BOOLEAN NOT NULL,
    telemetry BOOLEAN NOT NULL,
    update_channel VARCHAR(128) NOT NULL,
    upnp BOOLEAN NOT NULL,
    delete_nedb BOOLEAN NOT NULL,
    no_backups BOOLEAN NOT NULL,

    setup_id INTEGER UNIQUE,
    FOREIGN KEY (setup_id) REFERENCES setup(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS game_options (
    id INTEGER PRIMARY KEY,

    language VARCHAR(128) NOT NULL,
    update_channel VARCHAR(128) NOT NULL,
    port INTEGER NOT NULL,

    game_id INTEGER UNIQUE,
    FOREIGN KEY (game_id) REFERENCES game(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS macro (
    id TEXT PRIMARY KEY,

    command VARCHAR(128) NOT NULL,
    name VARCHAR(128) NOT NULL,
    type VARCHAR(128) NOT NULL,
    img VARCHAR(128) NOT NULL,
    author VARCHAR(128) NOT NULL,
    scope VARCHAR(128) NOT NULL,
    folder VARCHAR(128) NOT NULL,
    sort INTEGER NOT NULL,

    game_id INTEGER,
    FOREIGN KEY (game_id) REFERENCES game(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS macro (
    id TEXT PRIMARY KEY,

    command VARCHAR(128) NOT NULL,
    name VARCHAR(128) NOT NULL,
    type VARCHAR(128) NOT NULL,
    img VARCHAR(128) NOT NULL,
    author VARCHAR(128) NOT NULL,
    scope VARCHAR(128) NOT NULL,
    folder VARCHAR(128) NOT NULL,
    sort INTEGER NOT NULL,

    game_id INTEGER,
    FOREIGN KEY (game_id) REFERENCES game(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS playlist (
    id TEXT PRIMARY KEY,

    name VARCHAR(128) NOT NULL,
    folder VARCHAR(128) NOT NULL,
    sorting VARCHAR(128) NOT NULL,
    description VARCHAR(128) NOT NULL,
    channel VARCHAR(128) NOT NULL,
    mode INTEGER NOT NULL,
    fade INTEGER NOT NULL,
    seed INTEGER NOT NULL,
    sort INTEGER NOT NULL,
    playing BOOLEAN NOT NULL,

    game_id INTEGER,
    FOREIGN KEY (game_id) REFERENCES game(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS sound (
    id TEXT PRIMARY KEY,

    name VARCHAR(128) NOT NULL,
    path VARCHAR(128) NOT NULL,
    channel VARCHAR(128) NOT NULL,
    description VARCHAR(128) NOT NULL,
    fade INTEGER NOT NULL,
    sort INTEGER NOT NULL,
    repeat BOOLEAN NOT NULL,
    playing BOOLEAN NOT NULL,
    volume REAL NOT NULL,
    paused_time REAL NOT NULL,

    playlist_id TEXT,
    FOREIGN KEY (playlist_id) REFERENCES playlist(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS actor (
    id TEXT PRIMARY KEY,

    img VARCHAR(128) NOT NULL,
    name VARCHAR(128) NOT NULL,
    type VARCHAR(128) NOT NULL,
    folder VARCHAR(128) NOT NULL,
    sort INTEGER NOT NULL,

    game_id INTEGER,
    FOREIGN KEY (game_id) REFERENCES game(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS token (
    id INTEGER PRIMARY KEY,

    name VARCHAR(128) NOT NULL,
    actor_link BOOLEAN NOT NULL,
    append_number BOOLEAN NOT NULL,
    prepend_adjective BOOLEAN NOT NULL,
    lock_rotation BOOLEAN NOT NULL,
    random_img BOOLEAN NOT NULL,
    display_name INTEGER NOT NULL,
    display_bars INTEGER NOT NULL,
    disposition INTEGER NOT NULL,
    rotation INTEGER NOT NULL,
    alpha INTEGER NOT NULL,
    width REAL NOT NULL,
    height REAL NOT NULL,
    
    actor_id INTEGER UNIQUE,
    FOREIGN KEY (actor_id) REFERENCES actor(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS ring (
    id INTEGER PRIMARY KEY AUTOINCREMENT,

    enabled BOOLEAN NOT NULL,
    effects INTEGER NOT NULL,
    
    token_id INTEGER UNIQUE,
    FOREIGN KEY (token_id) REFERENCES token(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS ring_colors (
    id INTEGER PRIMARY KEY AUTOINCREMENT,

    ring VARCHAR(128) NOT NULL,
    background VARCHAR(128) NOT NULL,
    
    ring_id INTEGER UNIQUE,
    FOREIGN KEY (ring_id) REFERENCES ring(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS ring_subject (
    id INTEGER PRIMARY KEY AUTOINCREMENT,

    scale INTEGER NOT NULL,
    texture VARCHAR(128) NOT NULL,
    
    ring_id INTEGER UNIQUE,
    FOREIGN KEY (ring_id) REFERENCES ring(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS token_sight (
    id INTEGER PRIMARY KEY AUTOINCREMENT,

    color VARCHAR(128) NOT NULL,
    vision_mode VARCHAR(128) NOT NULL,
    range_ INTEGER NOT NULL,
    angle INTEGER NOT NULL,
    attenuation REAL NOT NULL,
    brightness REAL NOT NULL,
    enabled BOOLEAN NOT NULL,
    
    token_id INTEGER UNIQUE,
    FOREIGN KEY (token_id) REFERENCES token(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS token_light (
    id INTEGER PRIMARY KEY AUTOINCREMENT,

    color VARCHAR(128) NOT NULL,
    priority INTEGER NOT NULL,
    angle INTEGER NOT NULL,
    negative BOOLEAN NOT NULL,
    alpha REAL NOT NULL,
    bright REAL NOT NULL,
    coloration REAL NOT NULL,
    dim REAL NOT NULL,
    attenuation REAL NOT NULL,
    luminosity REAL NOT NULL,
    saturation REAL NOT NULL,
    contrast REAL NOT NULL,
    shadows REAL NOT NULL,
    
    token_id INTEGER UNIQUE,
    FOREIGN KEY (token_id) REFERENCES token(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS token_light_animation (
    id INTEGER PRIMARY KEY AUTOINCREMENT,

    speed INTEGER NOT NULL,
    intensity INTEGER NOT NULL,
    reverse BOOLEAN NOT NULL,

    token_light_id INTEGER UNIQUE,
    FOREIGN KEY (token_light_id) REFERENCES token_light(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS token_light_darkness (
    id INTEGER PRIMARY KEY AUTOINCREMENT,

    min REAL NOT NULL,
    max REAL NOT NULL,

    token_light_id INTEGER UNIQUE,
    FOREIGN KEY (token_light_id) REFERENCES token_light(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS token_texture (
    id INTEGER PRIMARY KEY AUTOINCREMENT,

    src VARCHAR(128) NOT NULL,
    fit VARCHAR(128) NOT NULL,
    tint VARCHAR(128) NOT NULL,
    scale_x REAL NOT NULL,
    scale_y REAL NOT NULL,
    offset_x REAL NOT NULL,
    offset_y REAL NOT NULL,
    rotation REAL NOT NULL,
    anchor_x REAL NOT NULL,
    anchor_y REAL NOT NULL,
    alpha_threshold REAL NOT NULL,
    
    token_id INTEGER UNIQUE,
    FOREIGN KEY (token_id) REFERENCES token(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS token_bar_1 (
    id INTEGER PRIMARY KEY AUTOINCREMENT,

    attribute VARCHAR(128) NOT NULL,
    
    token_id INTEGER UNIQUE,
    FOREIGN KEY (token_id) REFERENCES token(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS token_bar_2 (
    id INTEGER PRIMARY KEY AUTOINCREMENT,

    attribute VARCHAR(128) NOT NULL,
    
    token_id INTEGER UNIQUE,
    FOREIGN KEY (token_id) REFERENCES token(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS token_occludable (
    id INTEGER PRIMARY KEY AUTOINCREMENT,

    radius INTEGER NOT NULL,
    
    token_id INTEGER UNIQUE,
    FOREIGN KEY (token_id) REFERENCES token(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS token_turn_maker (
    id INTEGER PRIMARY KEY AUTOINCREMENT,

    mode INTEGER NOT NULL,
    animation VARCHAR(128) NOT NULL,
    src VARCHAR(128) NOT NULL,
    disposition BOOLEAN NOT NULL,
    
    token_id INTEGER UNIQUE,
    FOREIGN KEY (token_id) REFERENCES token(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS table_ (
    id TEXT PRIMARY KEY,

    name VARCHAR(128) NOT NULL,
    description VARCHAR(128) NOT NULL,
    formula VARCHAR(128) NOT NULL,
    img VARCHAR(128) NOT NULL,
    folder VARCHAR(128) NOT NULL,
    sort INTEGER NOT NULL,
    replacement BOOLEAN NOT NULL,
    display_roll BOOLEAN NOT NULL,

    game_id INTEGER,
    FOREIGN KEY (game_id) REFERENCES game(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS table_result (
    id TEXT PRIMARY KEY,

    type VARCHAR(128) NOT NULL,
    img VARCHAR(128) NOT NULL,
    description VARCHAR(128) NOT NULL,
    name VARCHAR(128) NOT NULL,
    weight INTEGER NOT NULL,
    drawn BOOLEAN NOT NULL,

    table_id INTEGER,
    FOREIGN KEY (table_id) REFERENCES table_(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS table_result_range (
    id INTEGER PRIMARY KEY AUTOINCREMENT,

    range_ INTEGER NOT NULL,

    table_result_id INTEGER,
    FOREIGN KEY (table_result_id) REFERENCES table_result(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS user (
    id TEXT PRIMARY KEY,

    name VARCHAR(128) NOT NULL,
    avatar VARCHAR(128) NOT NULL,
    character VARCHAR(128) NOT NULL,
    color VARCHAR(128) NOT NULL,
    pronouns VARCHAR(128) NOT NULL,
    role INTEGER NOT NULL,

    game_id INTEGER,
    FOREIGN KEY (game_id) REFERENCES game(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS setting (
    id TEXT PRIMARY KEY,

    key_ VARCHAR(128) NOT NULL,
    value VARCHAR(128) NOT NULL,

    game_id INTEGER,
    FOREIGN KEY (game_id) REFERENCES game(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS hotbar (
    id INTEGER PRIMARY KEY,

    key_ INTEGER NOT NULL,
    value VARCHAR(128) NOT NULL,

    user_id TEXT,
    FOREIGN KEY (user_id) REFERENCES user(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS active_users (
    id INTEGER PRIMARY KEY AUTOINCREMENT,

    users VARCHAR(128) NOT NULL,

    game_id INTEGER,
    FOREIGN KEY (game_id) REFERENCES game(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS grid (
    id INTEGER PRIMARY KEY AUTOINCREMENT,

    type INTEGER NOT NULL,
    size INTEGER NOT NULL,
    distance INTEGER NOT NULL,
    diagonals INTEGER NOT NULL,
    thickness INTEGER NOT NULL,
    alpha REAL NOT NULL,
    color VARCHAR(128) NOT NULL,
    units VARCHAR(128) NOT NULL,
    style VARCHAR(128) NOT NULL,

    system_id TEXT UNIQUE,
    FOREIGN KEY (system_id) REFERENCES system(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS relationships (
    id INTEGER PRIMARY KEY AUTOINCREMENT,

    module_id TEXT UNIQUE,
    FOREIGN KEY (module_id) REFERENCES module(id) ON DELETE CASCADE,
    system_id TEXT UNIQUE,
    FOREIGN KEY (system_id) REFERENCES system(id) ON DELETE CASCADE,
    world_id TEXT UNIQUE,
    FOREIGN KEY (world_id) REFERENCES world(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS release_ (
    id INTEGER PRIMARY KEY AUTOINCREMENT,

    generation INTEGER NOT NULL,
    channel VARCHAR(128) NOT NULL,
    suffix VARCHAR(128) NOT NULL,
    build INTEGER NOT NULL,
    node_version INTEGER NOT NULL,
    max_generation INTEGER NOT NULL,
    max_stable_generation INTEGER NOT NULL,
    time INTEGER NOT NULL,

    setup_id INTEGER UNIQUE,
    FOREIGN KEY (setup_id) REFERENCES setup(id) ON DELETE CASCADE,
    game_id INTEGER UNIQUE,
    FOREIGN KEY (game_id) REFERENCES game(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS item (
    id TEXT PRIMARY KEY,

    img VARCHAR(128) NOT NULL,
    name VARCHAR(128) NOT NULL,
    type VARCHAR(128) NOT NULL,
    folder VARCHAR(128) NOT NULL,
    sort INTEGER NOT NULL,

    game_id INTEGER,
    FOREIGN KEY (game_id) REFERENCES game(id) ON DELETE CASCADE,
    actor_id TEXT,
    FOREIGN KEY (actor_id) REFERENCES actor(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS journal (
    id TEXT PRIMARY KEY,

    name VARCHAR(128) NOT NULL,
    sort INTEGER NOT NULL,

    game_id INTEGER UNIQUE,
    FOREIGN KEY (game_id) REFERENCES game(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS relationships_data (
    id TEXT PRIMARY KEY,

    relationships_type VARCHAR(32) NOT NULL,
    type TEXT NOT NULL,
    manifest TEXT NOT NULL,

    relationships_id INTEGER UNIQUE,
    FOREIGN KEY (relationships_id) REFERENCES relationships(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS compatibility (
    id INTEGER PRIMARY KEY AUTOINCREMENT,

    minimum VARCHAR(64) NOT NULL,
    verified VARCHAR(64) NOT NULL,
    maximum VARCHAR(64) NOT NULL,

    module_id TEXT UNIQUE,
    FOREIGN KEY (module_id) REFERENCES module(id) ON DELETE CASCADE,
    system_id TEXT UNIQUE,
    FOREIGN KEY (system_id) REFERENCES system(id) ON DELETE CASCADE,
    world_id TEXT UNIQUE,
    FOREIGN KEY (world_id) REFERENCES world(id) ON DELETE CASCADE,
    relationships_data_id TEXT UNIQUE,
    FOREIGN KEY (relationships_data_id) REFERENCES relationships_data(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS author (
    id INTEGER PRIMARY KEY AUTOINCREMENT,

    name VARCHAR(128) NOT NULL,
    url VARCHAR(128) NOT NULL,
    email VARCHAR(128) NOT NULL,
    discord VARCHAR(128) NOT NULL,

    module_id TEXT,
    FOREIGN KEY (module_id) REFERENCES module(id) ON DELETE CASCADE,
    system_id TEXT,
    FOREIGN KEY (system_id) REFERENCES system(id) ON DELETE CASCADE,
    world_id TEXT,
    FOREIGN KEY (world_id) REFERENCES world(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS media (
    id INTEGER PRIMARY KEY AUTOINCREMENT,

    type VARCHAR(128) NOT NULL,
    url VARCHAR(128) NOT NULL,
    caption VARCHAR(128) NOT NULL,

    module_id TEXT,
    FOREIGN KEY (module_id) REFERENCES module(id) ON DELETE CASCADE,
    system_id TEXT,
    FOREIGN KEY (system_id) REFERENCES system(id) ON DELETE CASCADE,
    world_id TEXT,
    FOREIGN KEY (world_id) REFERENCES world(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS scripts (
    id INTEGER PRIMARY KEY AUTOINCREMENT,

    scripts TEXT NOT NULL,

    module_id TEXT,
    FOREIGN KEY (module_id) REFERENCES module(id) ON DELETE CASCADE,
    system_id INTEGER,
    FOREIGN KEY (system_id) REFERENCES system(id) ON DELETE CASCADE,
    world_id TEXT,
    FOREIGN KEY (world_id) REFERENCES world(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS es_modules (
    id INTEGER PRIMARY KEY AUTOINCREMENT,

    es_modules TEXT NOT NULL,

    module_id TEXT,
    FOREIGN KEY (module_id) REFERENCES module(id) ON DELETE CASCADE,
    system_id TEXT,
    FOREIGN KEY (system_id) REFERENCES system(id) ON DELETE CASCADE,
    world_id TEXT,
    FOREIGN KEY (world_id) REFERENCES world(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS style (
    id INTEGER PRIMARY KEY AUTOINCREMENT,

    src VARCHAR(128) NOT NULL,

    module_id TEXT,
    FOREIGN KEY (module_id) REFERENCES module(id) ON DELETE CASCADE,
    system_id TEXT,
    FOREIGN KEY (system_id) REFERENCES system(id) ON DELETE CASCADE,
    world_id TEXT,
    FOREIGN KEY (world_id) REFERENCES world(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS language (
    id INTEGER PRIMARY KEY AUTOINCREMENT,

    lang VARCHAR(128) NOT NULL,
    name VARCHAR(128) NOT NULL,
    path VARCHAR(128) NOT NULL,

    module_id TEXT,
    FOREIGN KEY (module_id) REFERENCES module(id) ON DELETE CASCADE,
    system_id TEXT,
    FOREIGN KEY (system_id) REFERENCES system(id) ON DELETE CASCADE,
    world_id TEXT,
    FOREIGN KEY (world_id) REFERENCES world(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS package_warnings (
    id TEXT PRIMARY KEY,

    key_ VARCHAR(128) NOT NULL,

    setup_id INTEGER,
    FOREIGN KEY (setup_id) REFERENCES setup(id) ON DELETE CASCADE,
    game_id INTEGER,
    FOREIGN KEY (game_id) REFERENCES game(id) ON DELETE CASCADE,
);

CREATE TABLE IF NOT EXISTS package_warnings_data (
    id TEXT PRIMARY KEY,

    type VARCHAR(128) NOT NULL,
    reinstallable VARCHAR(128) NOT NULL,
    manifest VARCHAR(128) NOT NULL,

    package_warnings_id TEXT,
    FOREIGN KEY (package_warnings_id) REFERENCES package_warnings(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS package_warnings_data_warning (
    id INTEGER PRIMARY KEY,

    value VARCHAR(128) NOT NULL,

    package_warnings_data_id TEXT,
    FOREIGN KEY (package_warnings_data_id) REFERENCES package_warnings_data(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS package_warnings_data_error (
    id INTEGER PRIMARY KEY,

    value VARCHAR(128) NOT NULL,

    package_warnings_data_id TEXT,
    FOREIGN KEY (package_warnings_data_id) REFERENCES package_warnings_data(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS world_folder (
    id TEXT PRIMARY KEY,

    name VARCHAR(128) NOT NULL,
    type VARCHAR(128) NOT NULL,
    folder VARCHAR(128) NOT NULL,
    sorting VARCHAR(128) NOT NULL,
    description VARCHAR(128) NOT NULL,
    color VARCHAR(128) NOT NULL,
    sort INTEGER NOT NULL,

    game_id INTEGER,
    FOREIGN KEY (game_id) REFERENCES game(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS message (
    id TEXT PRIMARY KEY,

    blind BOOLEAN NOT NULL,
    emote BOOLEAN NOT NULL,
    style INTEGER NOT NULL,
    timestamp DATETIME NOT NULL,
    content VARCHAR(128) NOT NULL,
    author VARCHAR(128) NOT NULL,
    type VARCHAR(128) NOT NULL,
    flavor VARCHAR(128) NOT NULL,
    sound VARCHAR(128) NOT NULL,

    game_id INTEGER,
    FOREIGN KEY (game_id) REFERENCES game(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS combat (
    id TEXT PRIMARY KEY,

    type VARCHAR(128) NOT NULL,
    scene VARCHAR(128) NOT NULL,
    round INTEGER NOT NULL,
    turn INTEGER NOT NULL,
    sort INTEGER NOT NULL,
    active INTEGER NOT NULL,

    game_id INTEGER,
    FOREIGN KEY (game_id) REFERENCES game(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS card_deck (
    id TEXT PRIMARY KEY,

    name VARCHAR(128) NOT NULL,
    type VARCHAR(128) NOT NULL,
    description VARCHAR(128) NOT NULL,
    img VARCHAR(128) NOT NULL,
    folder VARCHAR(128) NOT NULL,
    width INTEGER NOT NULL,
    height INTEGER NOT NULL,
    rotation INTEGER NOT NULL,
    sort INTEGER NOT NULL,
    display_count BOOLEAN NOT NULL,

    game_id INTEGER,
    FOREIGN KEY (game_id) REFERENCES game(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS combatant (
    id TEXT PRIMARY KEY,

    token_id VARCHAR(128) NOT NULL,
    scene_id VARCHAR(128) NOT NULL,
    actor_id VARCHAR(128) NOT NULL,
    type VARCHAR(128) NOT NULL,
    img VARCHAR(128) NOT NULL,
    group_ VARCHAR(128) NOT NULL,
    initiative INTEGER NOT NULL,
    hidden BOOLEAN NOT NULL,
    defeated BOOLEAN NOT NULL,

    combat_id TEXT,
    FOREIGN KEY (combat_id) REFERENCES combat(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS stats (
    id TEXT PRIMARY KEY,

    core_version VARCHAR(128) NOT NULL,
    system_id VARCHAR(128) NOT NULL,
    system_version VARCHAR(128) NOT NULL,
    last_modified_by VARCHAR(128) NOT NULL,
    modified_time DATETIME NOT NULL,

    message_id TEXT UNIQUE,
    FOREIGN KEY (message_id) REFERENCES message(id) ON DELETE CASCADE,
    combat_id TEXT UNIQUE,
    FOREIGN KEY (combat_id) REFERENCES combat(id) ON DELETE CASCADE,
    combatant_id TEXT UNIQUE,
    FOREIGN KEY (combatant_id) REFERENCES combatant(id) ON DELETE CASCADE,
    card_deck_id TEXT UNIQUE,
    FOREIGN KEY (card_deck_id) REFERENCES card_deck(id) ON DELETE CASCADE,
    card_id TEXT UNIQUE,
    FOREIGN KEY (card_id) REFERENCES card(id) ON DELETE CASCADE,
    user_id TEXT UNIQUE,
    FOREIGN KEY (user_id) REFERENCES user(id) ON DELETE CASCADE,
    macro_id TEXT UNIQUE,
    FOREIGN KEY (macro_id) REFERENCES macro(id) ON DELETE CASCADE,
    world_folder_id TEXT UNIQUE,
    FOREIGN KEY (world_folder_id) REFERENCES world_folder(id) ON DELETE CASCADE,
    item_id TEXT UNIQUE,
    FOREIGN KEY (item_id) REFERENCES item(id) ON DELETE CASCADE,
    setting_id TEXT UNIQUE,
    FOREIGN KEY (setting_id) REFERENCES setting(id) ON DELETE CASCADE,
    journal_page_id TEXT UNIQUE,
    FOREIGN KEY (journal_page_id) REFERENCES journal_page(id) ON DELETE CASCADE,
    table_id TEXT UNIQUE,
    FOREIGN KEY (table_id) REFERENCES table_(id) ON DELETE CASCADE,
    table_result_id TEXT UNIQUE,
    FOREIGN KEY (table_result_id) REFERENCES table_result(id) ON DELETE CASCADE,
    playlist_id TEXT UNIQUE,
    FOREIGN KEY (playlist_id) REFERENCES playlist(id) ON DELETE CASCADE,
    actor_id TEXT UNIQUE,
    FOREIGN KEY (actor_id) REFERENCES actor(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS speaker (
    id TEXT PRIMARY KEY,

    scene VARCHAR(128) NOT NULL,
    actor VARCHAR(128) NOT NULL,
    token VARCHAR(128) NOT NULL,
    alias VARCHAR(128) NOT NULL,

    message_id TEXT UNIQUE,
    FOREIGN KEY (message_id) REFERENCES message(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS combat_groups (
    id TEXT PRIMARY KEY,

    groups_ VARCHAR(128) NOT NULL,

    combat_id TEXT,
    FOREIGN KEY (combat_id) REFERENCES combat(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS card (
    id TEXT PRIMARY KEY,

    name VARCHAR(128) NOT NULL,
    type VARCHAR(128) NOT NULL,
    suit VARCHAR(128) NOT NULL,
    description VARCHAR(128) NOT NULL,
    origin VARCHAR(128) NOT NULL,
    width INTEGER NOT NULL,
    height INTEGER NOT NULL,
    rotation INTEGER NOT NULL,
    value INTEGER NOT NULL,
    face INTEGER NOT NULL,
    sort INTEGER NOT NULL,
    drawn BOOLEAN NOT NULL,

    card_deck_id TEXT,
    FOREIGN KEY (card_deck_id) REFERENCES card_deck(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS back (
    id INTEGER PRIMARY KEY AUTOINCREMENT,

    name VARCHAR(128) NOT NULL,
    text VARCHAR(128) NOT NULL,

    card_id TEXT UNIQUE,
    FOREIGN KEY (card_id) REFERENCES card(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS face (
    id INTEGER PRIMARY KEY AUTOINCREMENT,

    name VARCHAR(128) NOT NULL,
    img VARCHAR(128) NOT NULL,
    text VARCHAR(128) NOT NULL,

    card_id TEXT,
    FOREIGN KEY (card_id) REFERENCES card(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS message_whisper (
    id TEXT PRIMARY KEY,

    whisper VARCHAR(128) NOT NULL,

    message_id TEXT,
    FOREIGN KEY (message_id) REFERENCES message(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS message_rolls (
    id TEXT PRIMARY KEY,

    rolls VARCHAR(128) NOT NULL,

    message_id TEXT,
    FOREIGN KEY (message_id) REFERENCES message(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS pack (
    id TEXT PRIMARY KEY,

    name VARCHAR(128) NOT NULL,
    label VARCHAR(128) NOT NULL,
    banner VARCHAR(128) NOT NULL,
    path VARCHAR(128) NOT NULL,
    type VARCHAR(128) NOT NULL,
    system VARCHAR(128) NOT NULL,
    package_type VARCHAR(128) NOT NULL,
    package_name VARCHAR(128) NOT NULL,

    game_id INTEGER,
    FOREIGN KEY (game_id) REFERENCES game(id) ON DELETE CASCADE,
    module_id TEXT,
    FOREIGN KEY (module_id) REFERENCES module(id) ON DELETE CASCADE,
    system_id TEXT,
    FOREIGN KEY (system_id) REFERENCES system(id) ON DELETE CASCADE,
    world_id TEXT,
    FOREIGN KEY (world_id) REFERENCES world(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS journal_page (
    id TEXT PRIMARY KEY,

    name VARCHAR(128) NOT NULL,
    type VARCHAR(128) NOT NULL,
    src VARCHAR(128) NOT NULL,
    sort INTEGER NOT NULL,

    journal_id TEXT,
    FOREIGN KEY (journal_id) REFERENCES journal(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS journal_page_text (
    id INTEGER PRIMARY KEY AUTOINCREMENT,

    content VARCHAR(128) NOT NULL,
    markdown VARCHAR(128) NOT NULL,
    format INTEGER NOT NULL,

    journal_page_id TEXT,
    FOREIGN KEY (journal_page_id) REFERENCES journal_page(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS journal_page_title (
    id INTEGER PRIMARY KEY AUTOINCREMENT,

    show BOOLEAN NOT NULL,
    level INTEGER NOT NULL,

    journal_page_id TEXT,
    FOREIGN KEY (journal_page_id) REFERENCES journal_page(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS journal_page_video (
    id INTEGER PRIMARY KEY AUTOINCREMENT,

    controls BOOLEAN NOT NULL,
    volume REAL NOT NULL,

    journal_page_id TEXT,
    FOREIGN KEY (journal_page_id) REFERENCES journal_page(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS ownership (
    id INTEGER PRIMARY KEY AUTOINCREMENT,

    player VARCHAR(128) NOT NULL,
    trusted VARCHAR(128) NOT NULL,
    assistant VARCHAR(128) NOT NULL,

    pack_id TEXT UNIQUE,
    FOREIGN KEY (pack_id) REFERENCES pack(id) ON DELETE CASCADE,
    card_deck_id TEXT UNIQUE,
    FOREIGN KEY (card_deck_id) REFERENCES card_deck(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS ownership_string (
    id INTEGER PRIMARY KEY AUTOINCREMENT,

    key_ VARCHAR(128) NOT NULL,
    value INTEGER NOT NULL,

    macro_id TEXT,
    FOREIGN KEY (macro_id) REFERENCES macro(id) ON DELETE CASCADE,
    item_id TEXT,
    FOREIGN KEY (item_id) REFERENCES item(id) ON DELETE CASCADE,
    journal_id TEXT,
    FOREIGN KEY (journal_id) REFERENCES journal(id) ON DELETE CASCADE,
    playlist_id TEXT,
    FOREIGN KEY (playlist_id) REFERENCES playlist(id) ON DELETE CASCADE,
    actor_id TEXT,
    FOREIGN KEY (actor_id) REFERENCES actor(id) ON DELETE CASCADE
);


CREATE TABLE IF NOT EXISTS index_ (
    id TEXT PRIMARY KEY,

    folder VARCHAR(128) NOT NULL,
    img VARCHAR(128) NOT NULL,
    name VARCHAR(128) NOT NULL,
    type VARCHAR(128) NOT NULL,

    pack_id TEXT,
    FOREIGN KEY (pack_id) REFERENCES pack(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS pack_folder (
    id TEXT PRIMARY KEY,

    description VARCHAR(128) NOT NULL,
    name VARCHAR(128) NOT NULL,
    sort INTEGER NOT NULL,
    sorting VARCHAR(128) NOT NULL,
    type VARCHAR(128) NOT NULL,

    pack_id TEXT,
    FOREIGN KEY (pack_id) REFERENCES pack(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS folder (
    id INTEGER PRIMARY KEY AUTOINCREMENT,

    name VARCHAR(128) NOT NULL,
    sorting VARCHAR(128) NOT NULL,
    color VARCHAR(128) NOT NULL,

    folder_id INTEGER,
    FOREIGN KEY (folder_id) REFERENCES folder(id) ON DELETE CASCADE,
    module_id TEXT,
    FOREIGN KEY (module_id) REFERENCES module(id) ON DELETE CASCADE,
    system_id TEXT,
    FOREIGN KEY (system_id) REFERENCES system(id) ON DELETE CASCADE,
    world_id TEXT,
    FOREIGN KEY (world_id) REFERENCES world(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS folder_packs (
    id INTEGER PRIMARY KEY AUTOINCREMENT,

    packs VARCHAR(128) NOT NULL,

    folder_id INTEGER,
    FOREIGN KEY (folder_id) REFERENCES folder(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS tags (
    id INTEGER PRIMARY KEY AUTOINCREMENT,

    tags VARCHAR(128) NOT NULL,

    module_id TEXT,
    FOREIGN KEY (module_id) REFERENCES module(id) ON DELETE CASCADE
    system_id TEXT,
    FOREIGN KEY (system_id) REFERENCES system(id) ON DELETE CASCADE
    world_id TEXT,
    FOREIGN KEY (world_id) REFERENCES world(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS document_types (
    id INTEGER PRIMARY KEY AUTOINCREMENT,

    module_id TEXT UNIQUE,
    FOREIGN KEY (module_id) REFERENCES module(id) ON DELETE CASCADE
    system_id TEXT UNIQUE,
    FOREIGN KEY (system_id) REFERENCES system(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS document_types_data (
    id INTEGER PRIMARY KEY AUTOINCREMENT,

    type VARCHAR(64) NOT NULL,

    document_types_id INTEGER,
    FOREIGN KEY (document_types_id) REFERENCES document_types(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS document_types_data_html (
    id INTEGER PRIMARY KEY AUTOINCREMENT,

    html_fields TEXT NOT NULL,

    document_types_data_id INTEGER,
    FOREIGN KEY (document_types_data_id) REFERENCES document_types_data(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS system_update (
    id INTEGER PRIMARY KEY AUTOINCREMENT,

    has_update BOOLEAN NOT NULL DEFAULT FALSE,
    version VARCHAR(128) NOT NULL,

    game_id INTEGER UNIQUE,
    FOREIGN KEY (game_id) REFERENCES game(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS core_update (
    id INTEGER PRIMARY KEY AUTOINCREMENT,

    has_update BOOLEAN NOT NULL DEFAULT FALSE,
    can_update BOOLEAN NOT NULL DEFAULT FALSE,
    could_reach_website BOOLEAN NOT NULL DEFAULT FALSE,
    slow_response BOOLEAN NOT NULL DEFAULT FALSE,
    version VARCHAR(64) NOT NULL,
    channel VARCHAR(64) NOT NULL,
    will_disable_modules BOOLEAN NOT NULL DEFAULT FALSE,

    setup_id INTEGER UNIQUE,
    game_id INTEGER UNIQUE,
    FOREIGN KEY (setup_id) REFERENCES setup(id) ON DELETE CASCADE,
    FOREIGN KEY (game_id) REFERENCES game(id) ON DELETE CASCADE,
    CONSTRAINT one_parent_only CHECK (
        (setup_id IS NOT NULL AND game_id IS NULL) OR 
        (setup_id IS NULL AND game_id IS NOT NULL)
    )
);

CREATE TABLE IF NOT EXISTS featured_content (
    id INTEGER PRIMARY KEY AUTOINCREMENT,

    title VARCHAR(128) NOT NULL,
    caption VARCHAR(128) NOT NULL,
    url VARCHAR(128) NOT NULL,
    image VARCHAR(128) NOT NULL,

    setup_id INTEGER UNIQUE,
    FOREIGN KEY (setup_id) REFERENCES setup(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS news (
    id INTEGER PRIMARY KEY AUTOINCREMENT,

    title VARCHAR(64) NOT NULL,
    caption VARCHAR(64) NOT NULL,
    url VARCHAR(64) NOT NULL,
    image VARCHAR(64) NOT NULL,

    setup_id INTEGER,
    FOREIGN KEY (setup_id) REFERENCES setup(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS files (
    id INTEGER PRIMARY KEY AUTOINCREMENT,

    setup_id INTEGER UNIQUE,
    game_id INTEGER UNIQUE,
    FOREIGN KEY (setup_id) REFERENCES setup(id) ON DELETE CASCADE,
    FOREIGN KEY (game_id) REFERENCES game(id) ON DELETE CASCADE,
    CONSTRAINT one_parent_only CHECK (
        (setup_id IS NOT NULL AND game_id IS NULL) OR 
        (setup_id IS NULL AND game_id IS NOT NULL)
    )
);

CREATE TABLE IF NOT EXISTS files_storage (
    id INTEGER PRIMARY KEY AUTOINCREMENT,

    storage TEXT NOT NULL,

    files_id INTEGER,
    FOREIGN KEY (files_id) REFERENCES files(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS setup_language (
    id INTEGER PRIMARY KEY,

    label VARCHAR(128) NOT NULL,

    setup_id INTEGER,
    FOREIGN KEY (setup_id) REFERENCES setup(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS setup_language_module (
    id TEXT PRIMARY KEY,

    label VARCHAR(128) NOT NULL,
    path VARCHAR(128) NOT NULL,

    setup_language_id INTEGER,
    FOREIGN KEY (setup_language_id) REFERENCES setup_language(id) ON DELETE CASCADE
);



CREATE TABLE IF NOT EXISTS language (
    id INTEGER PRIMARY KEY AUTOINCREMENT,

    lang VARCHAR(128) NOT NULL,
    name VARCHAR(128) NOT NULL,
    path VARCHAR(128) NOT NULL,

    module_id TEXT,
    FOREIGN KEY (module_id) REFERENCES module(id) ON DELETE CASCADE,
    system_id INTEGER,
    FOREIGN KEY (system_id) REFERENCES system(id) ON DELETE CASCADE,
    world_id INTEGER,
    FOREIGN KEY (world_id) REFERENCES world(id) ON DELETE CASCADE
);

