-- model in public schema

CREATE SCHEMA IF NOT EXISTS PUBLIC;

---- drop all, then create all 
-- DROP TABLE IF EXISTS PUBLIC.MEASUREMENT;
-- DROP TABLE IF EXISTS PUBLIC.BAY;
-- DROP TABLE IF EXISTS PUBLIC.TOPOLOGIC;
-- DROP TABLE IF EXISTS PUBLIC.COMPONENT;
-- DROP TABLE IF EXISTS PUBLIC.PAGE;
-- DROP TABLE IF EXISTS PUBLIC.STATION;
-- DROP TABLE IF EXISTS PUBLIC.ZONE;
-- DROP TABLE IF EXISTS PUBLIC.GRID;
-- DROP TABLE IF EXISTS PUBLIC.COMPONENT_TYPE;
-- DROP TYPE IF EXISTS public.polar_coordinates;


CREATE TYPE PUBLIC.POLAR_COORDINATES AS (
	MAGNITUDE DOUBLE PRECISION,
	PHASE DOUBLE PRECISION
);

CREATE TABLE PUBLIC.COMPONENT_TYPE (
    ID BIGSERIAL PRIMARY KEY,
    TYPE VARCHAR(64) NOT NULL DEFAULT '',
    NAME VARCHAR(64) NOT NULL DEFAULT '',
    CONFIG JSONB NOT NULL DEFAULT '{}',

    UNIQUE (TYPE)
);

CREATE TABLE PUBLIC.GRID (
    ID BIGSERIAL PRIMARY KEY,
    NAME VARCHAR(64) NOT NULL DEFAULT '',
    DESCRIPTION VARCHAR(512) NOT NULL DEFAULT '',
    OP INTEGER NOT NULL DEFAULT -1,
    TS TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE PUBLIC.ZONE (
    ID BIGSERIAL PRIMARY KEY,
    GRID_ID BIGINT NOT NULL,
    NAME VARCHAR(64) NOT NULL DEFAULT '',
    DESCRIPTION VARCHAR(512) NOT NULL DEFAULT '',
    OP INTEGER NOT NULL DEFAULT -1,
    TS TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    FOREIGN KEY (GRID_ID) REFERENCES PUBLIC.GRID (ID)
);

CREATE TABLE PUBLIC.STATION (
    ID BIGSERIAL PRIMARY KEY,
    ZONE_ID BIGINT NOT NULL,
    NAME VARCHAR(64) NOT NULL DEFAULT '',
    DESCRIPTION VARCHAR(512) NOT NULL DEFAULT '',
    IS_LOCAL BOOLEAN NOT NULL DEFAULT FALSE,
    OP INTEGER NOT NULL DEFAULT -1,
    TS TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    FOREIGN KEY (ZONE_ID) REFERENCES PUBLIC.ZONE (ID)
);

CREATE TABLE PUBLIC.PAGE (
    ID BIGSERIAL PRIMARY KEY,
    TAG VARCHAR(32) NOT NULL DEFAULT '',
    NAME VARCHAR(64) NOT NULL DEFAULT '',
    LABEL JSONB NOT NULL DEFAULT '{}',
    CONTEXT JSONB NOT NULL DEFAULT '{}',
    DESCRIPTION VARCHAR(512) NOT NULL DEFAULT '',
    OP INTEGER NOT NULL DEFAULT -1,
    TS TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP 
);

CREATE TABLE PUBLIC.COMPONENT (
    ID BIGSERIAL PRIMARY KEY,
    GLOBAL_UUID UUID NOT NULL DEFAULT GEN_RANDOM_UUID(),
    NSPATH VARCHAR(32) NOT NULL DEFAULT '',
    TAG VARCHAR(32) NOT NULL DEFAULT '',
    NAME VARCHAR(64) NOT NULL DEFAULT '',
    MODEL_NAME VARCHAR(64) NOT NULL DEFAULT '',
    DESCRIPTION VARCHAR(512) NOT NULL DEFAULT '',
    GRID VARCHAR(64) NOT NULL DEFAULT '',
    ZONE VARCHAR(64) NOT NULL DEFAULT '',
    STATION VARCHAR(64) NOT NULL DEFAULT '',
    TYPE INTEGER NOT NULL DEFAULT -1,
    IN_SERVICE BOOLEAN NOT NULL DEFAULT FALSE,
    STATE INTEGER NOT NULL DEFAULT -1,
    STATUS INTEGER NOT NULL DEFAULT -1,
    CONNECTION JSONB NOT NULL DEFAULT '{}',
    LABEL JSONB NOT NULL DEFAULT '{}',
    CONTEXT JSONB NOT NULL DEFAULT '{}',
    OP INTEGER NOT NULL DEFAULT -1,
    TS TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP 
);

CREATE TABLE PUBLIC.TOPOLOGIC (
    ID BIGSERIAL PRIMARY KEY,
    UUID_FROM UUID NOT NULL,
    UUID_TO UUID NOT NULL,
    FROM_PIN UUID NOT NULL,
    TO_PIN UUID NOT NULL,
    FLAG INTEGER NOT NULL DEFAULT 0,
    DESCRIPTION VARCHAR(512) NOT NULL DEFAULT '',
    OP INTEGER NOT NULL DEFAULT -1,
    TS TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP 
);

CREATE TABLE PUBLIC.BAY (
    ID BIGSERIAL PRIMARY KEY,
  	NAME VARCHAR(64) NOT NULL DEFAULT '',
  	TYPE VARCHAR(64) NOT NULL DEFAULT '',
    GRID VARCHAR(64) NOT NULL DEFAULT '',
    ZONE VARCHAR(64) NOT NULL DEFAULT '',
    STATION VARCHAR(64) NOT NULL DEFAULT '',
  	FROM_NODE JSONB NOT NULL DEFAULT '[]', -- uuids
  	TO_NODE JSONB NOT NULL DEFAULT '[]', -- uuids
    OP INTEGER NOT NULL DEFAULT -1,
    TS TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP 
);

CREATE TABLE PUBLIC.MEASUREMENT (
    ID BIGSERIAL PRIMARY KEY,
    TAG VARCHAR(64) NOT NULL DEFAULT '',
    NAME VARCHAR(64) NOT NULL DEFAULT '',
    TYPE SMALLINT NOT NULL DEFAULT -1,
    DATA_SOURCE JSONB NOT NULL DEFAULT '{}', -- {'type':,'main_pos':'','sub_pos':''}
    SIZE INTEGER NOT NULL DEFAULT -1,
    BAY_ID BIGINT NOT NULL,
    COMPONENT_ID BIGINT NOT NULL,
    OP INTEGER NOT NULL DEFAULT -1,
    TS TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    FOREIGN KEY (BAY_ID) REFERENCES PUBLIC.BAY (ID),
    FOREIGN KEY (COMPONENT_ID) REFERENCES PUBLIC.COMPONENT (ID)
);
