-- 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,
    TAGNAME VARCHAR(32) NOT NULL DEFAULT '',
    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,

    UNIQUE (TAGNAME)
);

CREATE TABLE PUBLIC.ZONE (
    ID BIGSERIAL PRIMARY KEY,
    GRID_ID BIGINT NOT NULL,
    TAGNAME VARCHAR(32) NOT NULL DEFAULT '',
    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,

    UNIQUE (TAGNAME),

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

CREATE TABLE PUBLIC.STATION (
    ID BIGSERIAL PRIMARY KEY,
    ZONE_ID BIGINT NOT NULL,
    TAGNAME VARCHAR(32) NOT NULL DEFAULT '',
    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,

    UNIQUE (TAGNAME),

    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 (
    GLOBAL_UUID UUID PRIMARY KEY 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,
    CONTEXT JSONB NOT NULL DEFAULT '{}', 
    FLAG INTEGER NOT NULL DEFAULT 1,
    DESCRIPTION VARCHAR(512) NOT NULL DEFAULT '',
    OP INTEGER NOT NULL DEFAULT -1,
    TS TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP 
);

CREATE TABLE PUBLIC.BAY (
    BAY_UUID UUID PRIMARY KEY DEFAULT GEN_RANDOM_UUID(),
  	NAME VARCHAR(64) NOT NULL DEFAULT '',
    TAG VARCHAR(32) NOT NULL DEFAULT '',
  	TYPE VARCHAR(64) NOT NULL DEFAULT '',
    UNOM DOUBLE PRECISION NOT NULL DEFAULT -1,
    FLA DOUBLE PRECISION NOT NULL DEFAULT -1,
    CAPACITY DOUBLE PRECISION NOT NULL DEFAULT -1,
    DESCRIPTION VARCHAR(512) NOT NULL DEFAULT '',
    IN_SERVICE BOOLEAN NOT NULL DEFAULT FALSE,
    STATE INTEGER NOT NULL DEFAULT -1,
    GRID VARCHAR(64) NOT NULL DEFAULT '',
    ZONE VARCHAR(64) NOT NULL DEFAULT '',
    STATION VARCHAR(64) NOT NULL DEFAULT '',
    BUSINESS JSONB NOT NULL DEFAULT '{}', -- for Server
    CONTEXT JSONB NOT NULL DEFAULT '{}', -- for UI
  	FROM_UUIDS JSONB NOT NULL DEFAULT '[]', -- uuids
  	TO_UUIDS JSONB NOT NULL DEFAULT '[]', -- uuids
    DEV_PROTECT JSONB NOT NULL DEFAULT '[]', -- devices
    DEV_FAULT_RECORD JSONB NOT NULL DEFAULT '[]', -- devices
    DEV_STATUS JSONB NOT NULL DEFAULT '[]', -- devices
    DEV_DYN_SENSE JSONB NOT NULL DEFAULT '[]', -- devices
    DEV_INSTRUCT JSONB NOT NULL DEFAULT '[]', -- devices
    DEV_ETC JSONB NOT NULL DEFAULT '[]', -- devices
    COMPONENTS UUID[] NOT NULL DEFAULT '{}',
    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,
    SIZE INTEGER NOT NULL DEFAULT -1,
    DATA_SOURCE JSONB NOT NULL DEFAULT '{}', -- {"type":1,"main_pos":"","sub_pos":""}
    BAY_UUID UUID NOT NULL,
    COMPONENT_UUID UUID NOT NULL,
    OP INTEGER NOT NULL DEFAULT -1,
    TS TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    FOREIGN KEY (BAY_UUID) REFERENCES PUBLIC.BAY (BAY_UUID),
    FOREIGN KEY (COMPONENT_UUID) REFERENCES PUBLIC.COMPONENT (GLOBAL_UUID)
);
