-- This script was generated by the ERD tool in pgAdmin 4.

-- Please log an issue at https://redmine.postgresql.org/projects/pgadmin4/issues/new if you find any bugs, including reproduction steps.

BEGIN;

CREATE SCHEMA units;

CREATE SCHEMA vehicles;

CREATE SCHEMA players;

CREATE SCHEMA missions;

CREATE SCHEMA events;

CREATE TABLE
    IF NOT EXISTS units."State" (
        "timestamp" timestamp
        with
            time zone NOT NULL,
            mission_id integer NOT NULL,
            net_id text COLLATE pg_catalog."default" NOT NULL,
            player_uid text COLLATE pg_catalog."default" NOT NULL DEFAULT '-1':: integer,
            is_alive boolean NOT NULL,
            is_afk boolean,
            is_speaking smallint,
            unit_name text COLLATE pg_catalog."default" NOT NULL,
            side text COLLATE pg_catalog."default" NOT NULL,
            "position" point NOT NULL,
            direction numeric(2, 0) NOT NULL,
            health numeric(2, 0),
            traits text [] COLLATE pg_catalog."default",
            CONSTRAINT "UnitStates_pkey" PRIMARY KEY (
                net_id,
                "timestamp",
                player_uid,
                mission_id
            )
    );

COMMENT ON
TABLE
    units."State" IS 'Reflects a unit''s (soldier''s) state during a mission. Can be AI (player_uid = -1) or a player.';

CREATE TABLE
    IF NOT EXISTS units."Identity" (
        "timestamp" timestamp
        with
            time zone NOT NULL,
            mission_id integer NOT NULL,
            net_id text COLLATE pg_catalog."default" NOT NULL,
            player_uid text COLLATE pg_catalog."default" NOT NULL DEFAULT '-1':: integer,
            class_name text COLLATE pg_catalog."default" NOT NULL,
            display_name text,
            side text,
            role_description text COLLATE pg_catalog."default",
            traits text [] COLLATE pg_catalog."default",
            CONSTRAINT "Units_pkey" PRIMARY KEY (
                mission_id,
                net_id,
                "timestamp",
                player_uid
            )
    );

COMMENT ON
TABLE
    units."Identity" IS 'This is polled periodically during the mission to gather static and less-dynamic traits of units (soldiers).';

CREATE TABLE
    IF NOT EXISTS events."Chat" (
        id bigserial NOT NULL,
        "timestamp" timestamp
        with
            time zone NOT NULL,
            mission_id integer NOT NULL,
            player_uid text COLLATE pg_catalog."default" NOT NULL DEFAULT -1,
            channel integer,
            owner integer,
            name text COLLATE pg_catalog."default",
            "from" text COLLATE pg_catalog."default",
            text text COLLATE pg_catalog."default",
            "forcedDisplay" boolean,
            "isPlayerMessage" boolean,
            "sentenceType" integer,
            "chatMessageType" integer,
            CONSTRAINT "Chat_pkey" PRIMARY KEY (
                "timestamp",
                mission_id,
                player_uid,
                id
            ),
            UNIQUE (id)
    );

CREATE TABLE
    IF NOT EXISTS missions."Played" (
        id serial NOT NULL,
        start_time_utc timestamp
        with
            time zone NOT NULL,
            start_time_game timestamp
        with
            time zone NOT NULL,
            world_name text COLLATE pg_catalog."default" NOT NULL,
            briefing_name text COLLATE pg_catalog."default" NOT NULL,
            mission_name text COLLATE pg_catalog."default" NOT NULL,
            mission_name_source text COLLATE pg_catalog."default" NOT NULL,
            author text COLLATE pg_catalog."default" NOT NULL,
            server_name text COLLATE pg_catalog."default" NOT NULL,
            server_mods json,
            ace_medical boolean,
            radio_tfar boolean,
            radio_acre boolean,
            duration interval,
            version_addon integer [] NOT NULL,
            version_extension integer [] NOT NULL,
            tags text [] COLLATE pg_catalog."default",
            CONSTRAINT "MissionsRun_pkey" PRIMARY KEY (id, world_name, start_time_utc),
            CONSTRAINT "MissionsRun_id_start_time_utc_key" UNIQUE (id, start_time_utc),
            UNIQUE (id)
    );

COMMENT ON
TABLE
    missions."Played" IS 'Contains core mission data, recorded at mission init. The IDs here are referenced by many other areas to "place" them.';

CREATE TABLE
    IF NOT EXISTS players."ConnectionsMission" (
        id bigserial NOT NULL,
        "timestamp" timestamp
        with
            time zone NOT NULL,
            mission_id integer NOT NULL,
            player_uid text COLLATE pg_catalog."default" NOT NULL,
            event_type text COLLATE pg_catalog."default" NOT NULL,
            profile_name text COLLATE pg_catalog."default" NOT NULL,
            display_name text COLLATE pg_catalog."default" NOT NULL,
            steam_name text COLLATE pg_catalog."default" NOT NULL,
            is_jip boolean,
            CONSTRAINT "ConnectionsMission_pkey" PRIMARY KEY (
                player_uid,
                "timestamp",
                profile_name,
                mission_id,
                id
            ),
            UNIQUE (
                "timestamp",
                mission_id,
                player_uid,
                id
            )
    );

CREATE TABLE
    IF NOT EXISTS units."Inventory" (
        "timestamp" timestamp
        with
            time zone NOT NULL,
            mission_id integer NOT NULL,
            net_id text NOT NULL,
            player_uid text NOT NULL DEFAULT -1,
            inventory jsonb NOT NULL,
            PRIMARY KEY (
                "timestamp",
                net_id,
                mission_id,
                player_uid
            )
    );

COMMENT ON
TABLE
    units."Inventory" IS 'This is used to track the inventory state of units.';

CREATE TABLE
    IF NOT EXISTS players."ConnectionsServer" (
        "timestamp" timestamp
        with
            time zone NOT NULL,
            player_uid text COLLATE pg_catalog."default" NOT NULL,
            event_type text COLLATE pg_catalog."default" NOT NULL,
            profile_name text COLLATE pg_catalog."default" NOT NULL,
            display_name text COLLATE pg_catalog."default" NOT NULL,
            steam_name text COLLATE pg_catalog."default" NOT NULL,
            is_jip boolean,
            CONSTRAINT "PlayersObserved_pkey" PRIMARY KEY (
                player_uid,
                "timestamp",
                profile_name
            )
    );

CREATE TABLE
    IF NOT EXISTS missions."Environment" (
        "timestamp" timestamp
        with
            time zone NOT NULL,
            mission_id integer NOT NULL,
            fog numeric(2),
            overcast numeric(2),
            rain numeric(2),
            humidity numeric(2),
            waves numeric(2),
            "windDir" numeric(2),
            "windStr" numeric(2),
            gusts numeric(2),
            lightnings numeric(2),
            "moonIntensity" numeric(2),
            "moonPhase" numeric(2),
            "sunOrMoon" numeric(2),
            PRIMARY KEY ("timestamp", mission_id)
    );

COMMENT ON
TABLE
    missions."Environment" IS 'Contains periodically collected environmental information during missions.';

CREATE TABLE
    IF NOT EXISTS vehicles."Identity" (
        "timestamp" timestamp
        with
            time zone NOT NULL,
            mission_id integer NOT NULL,
            net_id text NOT NULL,
            class_name text NOT NULL,
            display_name text,
            customization jsonb,
            weapons jsonb,
            PRIMARY KEY (
                "timestamp",
                mission_id,
                net_id
            )
    );

COMMENT ON
TABLE
    vehicles."Identity" IS 'Periodically polled identity information about a vehicle.';

CREATE TABLE
    IF NOT EXISTS vehicles."State" (
        "timestamp" timestamp
        with
            time zone NOT NULL,
            mission_id integer NOT NULL,
            net_id text NOT NULL,
            is_alive boolean,
            side text,
            "position" point NOT NULL,
            direction numeric(2),
            health numeric(2),
            crew text [],
            PRIMARY KEY (
                "timestamp",
                mission_id,
                net_id
            )
    );

COMMENT ON
TABLE
    vehicles."State" IS 'State information about vehicles. Crew is an array of net_id to be referenced against the Unit tables.';

CREATE TABLE
    IF NOT EXISTS players."AllObserved" (
        id serial NOT NULL,
        player_uid text NOT NULL,
        PRIMARY KEY (id, player_uid),
        UNIQUE (player_uid)
    );

ALTER TABLE
    IF EXISTS units."State"
ADD
    FOREIGN KEY (mission_id) REFERENCES missions."Played" (id) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION NOT VALID;

ALTER TABLE
    IF EXISTS units."State"
ADD
    FOREIGN KEY (player_uid) REFERENCES players."AllObserved" (player_uid) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION NOT VALID;

ALTER TABLE
    IF EXISTS units."Identity"
ADD
    FOREIGN KEY (mission_id) REFERENCES missions."Played" (id) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION NOT VALID;

ALTER TABLE
    IF EXISTS units."Identity"
ADD
    FOREIGN KEY (player_uid) REFERENCES players."AllObserved" (player_uid) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION NOT VALID;

ALTER TABLE
    IF EXISTS events."Chat"
ADD
    FOREIGN KEY (mission_id) REFERENCES missions."Played" (id) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION NOT VALID;

ALTER TABLE
    IF EXISTS events."Chat"
ADD
    FOREIGN KEY (player_uid) REFERENCES players."AllObserved" (player_uid) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION NOT VALID;

ALTER TABLE
    IF EXISTS players."ConnectionsMission"
ADD
    FOREIGN KEY (mission_id) REFERENCES missions."Played" (id) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION NOT VALID;

ALTER TABLE
    IF EXISTS players."ConnectionsMission"
ADD
    FOREIGN KEY (player_uid) REFERENCES players."AllObserved" (player_uid) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION NOT VALID;

ALTER TABLE
    IF EXISTS units."Inventory"
ADD
    FOREIGN KEY (mission_id) REFERENCES missions."Played" (id) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION NOT VALID;

ALTER TABLE
    IF EXISTS units."Inventory"
ADD
    FOREIGN KEY (player_uid) REFERENCES players."AllObserved" (player_uid) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION NOT VALID;

ALTER TABLE
    IF EXISTS missions."Environment"
ADD
    FOREIGN KEY (mission_id) REFERENCES missions."Played" (id) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION NOT VALID;

ALTER TABLE
    IF EXISTS vehicles."Identity"
ADD
    FOREIGN KEY (mission_id) REFERENCES missions."Played" (id) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION NOT VALID;

ALTER TABLE
    IF EXISTS vehicles."State"
ADD
    FOREIGN KEY (mission_id) REFERENCES missions."Played" (id) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION NOT VALID;

END;