CREATE DATABASE IF NOT EXISTS poconsole;

CREATE TABLE IF NOT EXISTS poconsole.Event_List (
  id int(11) NOT NULL AUTO_INCREMENT,
  alarmID int(11),
  type varchar(64),
  cond varchar(64),
  value float,
  datetime datetime,
  stroke_number int(11),
  PRIMARY KEY (id)
);

CREATE TABLE IF NOT EXISTS poconsole.Hist_Day (
  id int(11) NOT NULL AUTO_INCREMENT,
  gauge_date datetime,
  percent_run float,
  kWh float,
  kWh_regen float,
  electricity_cost float,
  peak_load float,
  min_load float,
  average_SPM float,
  production_calculated float,
  full_card_production float,
  polished_rod_HP float,
  lifting_cost float,
  fluid_above_pump float,
  pump_intake_pressure float,
  inflow_rate float,
  PRIMARY KEY (id)
);

CREATE TABLE IF NOT EXISTS poconsole.Well_Test (
  id int(11) NOT NULL AUTO_INCREMENT,
  test_date datetime,
  test_volume_oil float,
  test_volume_water float,
  test_volume_gas float,
  k_factor float,
  projected_volume_oil float,
  projected_volume_water float,
  api_gravity_oil float,
  sg_oil float,
  sg_water float,
  test_hours float,
  deleted int(11) DEFAULT 0,
  PRIMARY KEY (id)
);

-- CREATE TABLE IF NOT EXISTS poconsole.config (
--   id int(11) NOT NULL AUTO_INCREMENT,
--   device_type varchar(64),
--   ip_address varchar(64),
--   dateChanged datetime DEFAULT NOW(),
--   PRIMARY KEY (id)
-- );

CREATE TABLE IF NOT EXISTS poconsole.config (
    id INT NOT NULL AUTO_INCREMENT,
    parameter varchar(128),
    val varchar(128),
    dateAdded TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (id)
);

CREATE TABLE IF NOT EXISTS poconsole.Notes (
  id int(11) NOT NULL AUTO_INCREMENT,
  author varchar(64),
  note varchar(64),
  datetime datetime DEFAULT NOW(),
  type int(11),
  associated_stroke int(11),
  deleted int(11) DEFAULT 0,
  PRIMARY KEY (id)
);

CREATE TABLE IF NOT EXISTS poconsole.note_types(
  id int(11) NOT NULL AUTO_INCREMENT,
  type varchar(64),
  deleted int(11) DEFAULT 0,
  PRIMARY KEY (id)
);


INSERT INTO poconsole.note_types (id, type) VALUES ('1', 'Downtime Explanation');
INSERT INTO poconsole.note_types (id, type) VALUES ('2', 'Configuration Change');
INSERT INTO poconsole.note_types (id, type) VALUES ('3', 'Info');
INSERT INTO poconsole.note_types (id, type) VALUES ('4', 'Other');


CREATE TABLE IF NOT EXISTS poconsole.fluid_shot(
  id int(11) NOT NULL AUTO_INCREMENT,
  shot_datetime datetime,
  taken_by varchar(64),
  pump_intake_pressure float,
  fluid_gradient float,
  friction float,
  deleted int(11) DEFAULT 0,
  PRIMARY KEY (id)
);

CREATE TABLE IF NOT EXISTS poconsole.card_history(
  id int(11) NOT NULL AUTO_INCREMENT,
  Stroke_Time datetime,
  Card_id int(11),
  Card_Type varchar(64),
  Surface_Position varchar(4000),
  Surface_Load varchar(4000),
  Downhole_Position varchar(4000),
  Downhole_Load varchar(4000),
  PRIMARY KEY (id)
);

CREATE TABLE IF NOT EXISTS poconsole.card_history_dates(
  id int(11) NOT NULL AUTO_INCREMENT,
  year int(11),
  month int(11),
  day int(11),
  first_id int(11),
  PRIMARY KEY (id)
);

CREATE TABLE IF NOT EXISTS poconsole.tag_classes(
    id int(11) NOT NULL AUTO_INCREMENT,
    tag_class varchar(64),
    description varchar(64),
    PRIMARY KEY (id)
);

CREATE TABLE IF NOT EXISTS poconsole.tags(
    id int(11) NOT NULL AUTO_INCREMENT,
    name varchar(128),
    class int(11),
    tag varchar(128),
    description varchar(128),
    data_type varchar(32),
    change_threshold float,
    guarantee_sec integer(11),
    map_function varchar(64),
    units varchar(64),
    minExpected varchar(64),
    maxExpected varchar(64), 
    deleted INT NULL DEFAULT 0,
    PRIMARY KEY (id)
);


CREATE TABLE IF NOT EXISTS poconsole.tag_vals(
  id int(11) NOT NULL AUTO_INCREMENT,
  dtime datetime,
  name varchar(128),
  val float,
  PRIMARY KEY (id)
);

CREATE TABLE IF NOT EXISTS poconsole.well_config(
  id int(11) NOT NULL AUTO_INCREMENT,
  tstamp datetime,
  type varchar(64),
  val varchar(64),
  PRIMARY KEY (id)
);

CREATE TABLE IF NOT EXISTS poconsole.run_status(
  id int(11) NOT NULL AUTO_INCREMENT,
  dtime datetime,
  status varchar(64),
  PRIMARY KEY (id)
);

CREATE TABLE IF NOT EXISTS poconsole.alarm_classes(
    id int(11) NOT NULL AUTO_INCREMENT,
    alarm_class varchar(64),
    description varchar(128),
    PRIMARY KEY (id)
);

CREATE TABLE IF NOT EXISTS poconsole.alarms(
    id int(11) NOT NULL AUTO_INCREMENT,
    name varchar(64),
    class int(11),
    tag varchar(128),
    cond varchar(64),
    PRIMARY KEY (id)
);

INSERT INTO tag_classes (id, tag_class, description) VALUES (1, 'stroke', 'Stroke Information');
INSERT INTO tag_classes (id, tag_class, description) VALUES (2, 'history', 'Historical Data');
INSERT INTO tag_classes (id, tag_class, description) VALUES (3, 'gaugeoff', 'Gauge Off Data');
INSERT INTO tag_classes (id, tag_class, description) VALUES (4, 'welltest', 'Well Test Data');
INSERT INTO tag_classes (id, tag_class, description) VALUES (5, 'custom', 'Custom tags');

INSERT INTO alarm_classes(id, alarm_class, description) VALUES (1, 'analog', 'Analog Alarms');
INSERT INTO alarm_classes(id, alarm_class, description) VALUES (2, 'bit', 'Bit Statuses');


CREATE USER 'website'@'localhost' IDENTIFIED BY 'henrypump';
GRANT ALL ON *.* TO 'website'@'localhost';
CREATE USER 'admin'@'localhost' IDENTIFIED BY 'henrypump';
GRANT ALL ON *.* to 'admin'@'localhost';
CREATE USER 'admin'@'%' IDENTIFIED BY 'henrypump';
GRANT ALL ON *.* to 'admin'@'%';
FLUSH PRIVILEGES;
