------------------------------ version tables ---------------------------------

-------------------table "a".kpi;----------------------------------------------------------
CREATE TABLE IF NOT EXISTS "a".kpi
(
    id serial,
    tsup_mean_ep numeric,
    tsup_max_ep numeric,
    tsup_min_ep numeric,
    tret_mean_ep numeric,
    tret_max_ep numeric,
    tret_min_ep numeric,
    qsup_heat_ep numeric,
    qsup_cold_ep numeric,
    qsup_ep numeric,
    tsup_mean_c numeric,
    tsup_max_c numeric,
    tsup_min_c numeric,
    tret_mean_c numeric,
    tret_max_c numeric,
    tret_min_c numeric,
    qsup_heat_c numeric,
    qsup_cold_c numeric,
    qsup_c numeric,
    qamb numeric,
    CONSTRAINT kpi_pkey PRIMARY KEY (id)
);

-------------------table "a".boreholes;----------------------------------------------------------
DROP TABLE IF EXISTS "a".boreholes CASCADE;
CREATE TABLE "a".boreholes(
    id serial,
    geom GEOMETRY (POINTZ,25832),
    plant_id int DEFAULT 1,
    "group" int DEFAULT 1,
    mir boolean DEFAULT FALSE,
    CONSTRAINT boreholes_pkey PRIMARY KEY (id)
);

-------------------table "a".borehole_fields;----------------------------------------------------------
DROP TABLE IF EXISTS "a".borehole_fields CASCADE;
CREATE TABLE "a".borehole_fields(
    id serial,
    ep_id integer,
    zhole numeric CHECK (zhole > 0),
    rhole numeric CHECK (rhole > 0),
    rb numeric CHECK (rb >= 0), 
    rpipeearth numeric CHECK (rpipeearth > 0), 
    rpipegrout numeric CHECK (rpipegrout > 0), 
    rringearth numeric CHECK (rringearth > 0), 
    rgroutearth numeric CHECK (rgroutearth > 0), 
    rgroutgrout numeric CHECK (rgroutgrout > 0), 
    mir int,
    rmax numeric CHECK (rmax > 0),
    nring int CHECK (nring > 0),
    nzhole int CHECK (nzhole > 0),
    nlayt int CHECK (nlayt > 0),
    n1 int CHECK (n1 >= 0),
    n2 int CHECK (n2 >= 0),
    n3 int CHECK (n3 >= 0),
    toutput numeric CHECK (toutput >= 0),
    cpgrd numeric CHECK (cpgrd > 0),
    lambgrd numeric CHECK (lambgrd > 0),
    rhogrd numeric CHECK (rhogrd > 0),
    cpgrout numeric CHECK (cpgrout > 0),
    lambgrout numeric CHECK (lambgrout > 0),
    rhogrout numeric CHECK (rhogrout > 0),
    rpipe numeric CHECK (rpipe > 0),
    thickpipe numeric CHECK (thickpipe > 0),
    cppipe numeric CHECK (cppipe > 0),
    lambpipe numeric CHECK (lambpipe > 0),    
    lcasting numeric CHECK (lcasting > 0),
    lambda numeric CHECK (lambda > 0),
    rhosurface numeric CHECK (rhosurface > 0),
    cpsurface numeric CHECK (cpsurface > 0),  
    liqtype int,
    tfreeze numeric,
    lambliq numeric CHECK (lambliq > 0),
    tmean numeric,
    geotgrad numeric,
    CONSTRAINT borehole_fields_pkey PRIMARY KEY (id)
);

-------------------table "a".climate;------------------------------------
DROP TABLE IF EXISTS "a".climate CASCADE;
CREATE TABLE "a".climate
(
  id serial,
  name text DEFAULT 'Solna',
  file_name text DEFAULT 'C:\Program Files (x86)\IDA districts\climate\BROMMA.PRN',
  latitude numeric DEFAULT 59.366,
  longitude numeric DEFAULT -17.9999,
  timezone integer DEFAULT -1,
  height numeric DEFAULT 27,
  CONSTRAINT climate_pkey PRIMARY KEY (id)
);

-------------------table "a".submodels;------------------------------------
DROP TABLE IF EXISTS "a".submodels CASCADE;
CREATE TABLE "a".submodels
(
  id serial,
  submodel text,
  geom geometry(MultiPolygon,25832),
  CONSTRAINT submodel_pkey PRIMARY KEY (id)
);

-------------------table "a".network;------------------------------------
DROP TABLE IF EXISTS "a".network CASCADE;
CREATE TABLE "a".network
(
  id serial,
  description text,
  CONSTRAINT network_pkey PRIMARY KEY (id)
);

-------------------table "a".customers;------------------------------------
DROP TABLE IF EXISTS "a".customers CASCADE;
CREATE TABLE "a".customers
(
	id serial,
	geom geometry(PointZ,25832),
	template integer DEFAULT 1,
    network integer[] DEFAULT ARRAY[1],
	submodel integer DEFAULT 1, --reference to submodelname in subnet_line 
	CONSTRAINT customers_pkey PRIMARY KEY (id)
);


-------------------table "a".energy_plants;------------------------------------
DROP TABLE IF EXISTS "a".energy_plants CASCADE;
CREATE TABLE "a".energy_plants
(
	id serial,
	geom geometry(PointZ,25832),
	template integer DEFAULT 1,
    network integer[] DEFAULT ARRAY[1],
	submodel integer DEFAULT 1, --reference to submodelname in subnet_line 
	CONSTRAINT energy_plants_pkey PRIMARY KEY (id)
);


-------------------table "a".lines;------------------------------------
DROP TABLE IF EXISTS "a".lines CASCADE;
CREATE TABLE "a".lines
(
	id serial,
	geom geometry(LineStringZ,25832),
    network integer DEFAULT 1,
	type integer DEFAULT 0,
    pipe_bundle_type_id integer DEFAULT 1,
    zeta numeric DEFAULT 0,
	submodel integer[] DEFAULT ARRAY[1],
	CONSTRAINT lines_pkey PRIMARY KEY (id)
);

-------------------table "a".junctions------------------------------------
DROP TABLE IF EXISTS "a".junctions CASCADE;
CREATE TABLE "a".junctions
(
	id serial,
	geom geometry(PointZ,25832),
	type integer,
    n_connections integer check(n_connections >= 1),
	submodel integer DEFAULT 1, --reference to submodelname in subnet_line 
    network integer DEFAULT 1, --reference to submodelname in subnet_line 
    zeta numeric DEFAULT 0,
	CONSTRAINT junctions_pkey PRIMARY KEY (id)
);

-- Table: "a".buildings
-- DROP TABLE "a".buildings;
CREATE TABLE "a".buildings
(
  id serial,
  b_id integer,
  z_id integer,
  substation_id integer,
  submodel integer DEFAULT 1,
  geom geometry(MultiPolygon,25832),
  z_bh_m numeric,
  z_height_m numeric,
  CONSTRAINT buildings_pkey PRIMARY KEY (id)
);

-- Table: "a".junction_connections
-- DROP TABLE "a".junction_connections;
CREATE TABLE "a".junction_connections
(
  id serial,
  jid integer,
  lid integer,
  CONSTRAINT junction_connections_pkey PRIMARY KEY (id)
);

-- Table: "a".customer_connections
-- DROP TABLE "a".customer_connections;
CREATE TABLE "a".customer_connections
(
  id serial,
  cid integer,
  c_seq integer,
  lid integer,
  CONSTRAINT customer_connections_pkey PRIMARY KEY (id)
);
  
-- Table: "a".energy_plant_connections
-- DROP TABLE "a".energy_plant_connections;
CREATE TABLE "a".energy_plant_connections
(
  id serial,
  epid integer,
  ep_seq integer,
  lid integer,
  CONSTRAINT energy_plant_connections_pkey PRIMARY KEY (id)
);

-- Table: "a".pipes_model
-- DROP TABLE "a".pipes_model;
CREATE TABLE "a".pipes_model
(
  id serial,
  sim_model integer,
  pid integer,
  co_sim integer,
  CONSTRAINT id_pkey PRIMARY KEY (id)
);

-- Table: "a".segment_lines_00
-- DROP TABLE "a".segment_lines_00;
CREATE TABLE "a".segment_lines_00
(
  lines_id integer,
  rc_split_multi geometry
);

-- Index: "a".segment_lines_00_rc_split_multi_idx
-- DROP INDEX "a".segment_lines_00_rc_split_multi_idx;
CREATE INDEX segment_lines_00_rc_split_multi_idx
  ON "a".segment_lines_00
  USING gist
  (rc_split_multi);

-- Table: "a".streets
-- DROP TABLE "a".streets;
CREATE TABLE "a".streets
(
  id serial,
  geom geometry(LineString,25832),
  costs_eur7m numeric DEFAULT 100,
  source integer,
  target integer,
  CONSTRAINT streets_pkey PRIMARY KEY (id)
);

	
-- Table: "a".time_manager_tair
-- DROP TABLE "a".time_manager_tair;
CREATE TABLE "a".time_manager_tair
(
  id serial,
  b_id serial,
  the_geom geometry(MultiPolygon,25832),
  time_h text,
  time_stamp timestamp without time zone,
  t_air_c numeric,
  CONSTRAINT time_manager_tair_pkey PRIMARY KEY (id)
);

-- Table: "a".invoked_sf
-- DROP TABLE IF EXISTS "a".invoked_sf;
CREATE TABLE IF NOT EXISTS "a".invoked_sf
(
    id serial,
    sf text,
    type text,
    vars text[],
    CONSTRAINT invoked_sf_pkey PRIMARY KEY (id)
);