CREATE TABLE beacon_listener (
    listener_id integer PRIMARY KEY AUTOINCREMENT NOT NULL,

    port int NOT NULL,
    public_ip varchar NOT NULL,
    domain_name varchar NOT NULL,
    certificate varchar NOT NULL,
    privkey varchar NOT NULL
);

CREATE TABLE beacon_config (
    config_id integer PRIMARY KEY AUTOINCREMENT NOT NULL,

    mode text check (mode in ('single', 'regular', 'random', 'cron')),

    regular_interval int,

    random_min_time int,
    random_max_time int,

    cron_schedule varchar
);

CREATE TABLE beacon_category (
    category_id integer PRIMARY KEY AUTOINCREMENT NOT NULL,
    category_name varchar NOT NULL
);

CREATE TABLE beacon_template (
    template_id integer PRIMARY KEY AUTOINCREMENT NOT NULL,
    template_name varchar NOT NULL,
    operating_system varchar NOT NULL,
    config_id int NOT NULL,
    listener_id int NOT NULL,

    source_ip varchar NOT NULL,
    source_mac varchar,
    source_mode text check (source_mode in ('host', 'custom')),
    source_netmask int,
    source_gateway varchar,

    default_category int,

    FOREIGN KEY (config_id) REFERENCES beacon_config,
    FOREIGN KEY (listener_id) REFERENCES beacon_listener,
    FOREIGN KEY (default_category) REFERENCES beacon_category
);

CREATE TABLE beacon_instance (
    beacon_id varchar PRIMARY KEY NOT NULL,
    template_id int NOT NULL,
    peer_ip varchar NOT NULL,
    nickname varchar NOT NULL,

    cwd varchar NOT NULL,
    operating_system varchar NOT NULL,
    beacon_userent varchar NOT NULL,
    hostname varchar NOT NULL,

    FOREIGN KEY (template_id) REFERENCES beacon_template
);

CREATE TABLE beacon_category_assignment (
    category_id int,
    beacon_id varchar,

    PRIMARY KEY (category_id, beacon_id),

    FOREIGN KEY (category_id) REFERENCES beacon_category,
    FOREIGN KEY (beacon_id) REFERENCES beacon_instance
);

CREATE TABLE beacon_config_assignment (
    config_id int,
    beacon_id varchar,

    PRIMARY KEY (config_id, beacon_id),

    FOREIGN KEY (config_id) REFERENCES beacon_config,
    FOREIGN KEY (beacon_id) REFERENCES beacon_instance
);

CREATE TABLE beacon_command (
    command_id integer PRIMARY KEY AUTOINCREMENT NOT NULL,

    cmd_type text check (cmd_type in ('update', 'exec', 'install', 'upload', 'download', 'reload_config', 'chdir', 'ls')),

    exec_command varchar,

    install_target varchar,

    upload_src varchar,
    upload_dest varchar,

    download_path varchar,

    config_id int,

    FOREIGN KEY (config_id) REFERENCES beacon_config
);

CREATE TABLE beacon_command_invocation (
    beacon_id varchar NOT NULL,
    command_id int NOT NULL,

    invoker_id int NOT NULL,

    invocation_date int,
    invocation_result varchar,

    PRIMARY KEY (beacon_id, command_id),

    FOREIGN KEY (command_id) REFERENCES beacon_command,
    FOREIGN KEY (beacon_id) REFERENCES beacon_instance,
    FOREIGN KEY (invoker_id) REFERENCES users
);

CREATE TABLE beacon_checkin (
    beacon_id varchar NOT NULL,

    checkin_date int,

    FOREIGN KEY (beacon_id) REFERENCES beacon_instance
);
