CREATE TABLE versions (
  converter TEXT
);

CREATE TABLE ps_boards (
  id INTEGER NOT NULL PRIMARY KEY,
  daughterboard_id INTEGER
);

CREATE TABLE qaqc_single_run_results (
  id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  runid INTEGER NOT NULL,
  psboard_id INTEGER NOT NULL,
  daughterboard_id INTEGER,
  position INTEGER,
  resistance_test_passed BOOLEAN NOT NULL,
  qspip INTEGER,
  recov INTEGER,
  power INTEGER,
  clock INTEGER,
  asdtp INTEGER,
  reset INTEGER,
  qaqc_result INTEGER,
  lvds_tx_skew REAL,
  power_3v3d REAL,
  power_3v3a REAL,
  power_n3va REAL,
  note TEXT,
  is_slavelog_valid BOOLEAN,
  is_slaveclocklog_valid BOOLEAN,
  FOREIGN KEY("runid") REFERENCES "qaqc_runs"("id"),
  FOREIGN KEY("psboard_id") REFERENCES "ps_boards"("id"),
  FOREIGN KEY("position") REFERENCES "qaqc_positions"("id")
);

CREATE TABLE qaqc_runs (
  id INTEGER NOT NULL PRIMARY KEY,
  campaign_id INTEGER,
  run_datetime DATETIME,
  note TEXT,
  shifter TEXT NOT NULL,
  logfile TEXT,
  shiftscript_ver TEXT,
  FOREIGN KEY("campaign_id") REFERENCES "qaqc_campaigns"("id")
);

CREATE TABLE qaqc_dispatch (
  id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  qaqc_campaign_id INTEGER,
  psb_id INTEGER,
  source_place TEXT NOT NULL,
  destination TEXT NOT NULL,
  time DATETIME,
  FOREIGN KEY("qaqc_campaign_id") REFERENCES "qaqc_campaigns"("id"),
  FOREIGN KEY("psb_id") REFERENCES "ps_boards"("id")
);

CREATE TABLE qaqc_campaigns (
  id INTEGER NOT NULL PRIMARY KEY,
  start_date DATETIME NOT NULL,
  end_date DATETIME NOT NULL,
  note TEXT
);

CREATE TABLE qaqc_resistance_check (
  id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  psb_id INTEGER NOT NULL,
  passed BOOLEAN,
  FOREIGN KEY("psb_id") REFERENCES "ps_boards"("id")
);

CREATE TABLE qaqc_extra_run_results (
  id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  runid INTEGER,
  psboard_id INTEGER NOT NULL,
  position INTEGER,
  num_tests INTEGER,
  insufficient_reset_with_10 INTEGER,
  reset_failed_though_reconfig_done INTEGER,
  always_hit_flag_true INTEGER,
  dac_is_0 INTEGER,
  bcid_shift INTEGER,
  efficiency_99percent INTEGER,
  bcid_fail_111 INTEGER,
  bcid_fail_000 INTEGER,
  low_efficiency INTEGER,
  bcid_fail INTEGER,
  invalid_register_value INTEGER,
  power_out_of_range INTEGER,
  note TEXT,
  is_slavelog_valid BOOLEAN,
  FOREIGN KEY("runid") REFERENCES "qaqc_runs"("id"),
  FOREIGN KEY("psboard_id") REFERENCES "ps_boards"("id"),
  FOREIGN KEY("position") REFERENCES "qaqc_positions"("id")
);

-- TODO: add table for descriptions of each error?

CREATE TABLE qaqc_positions (
  id INTEGER NOT NULL PRIMARY KEY,
  name TEXT NOT NULL UNIQUE,
  station INTEGER NOT NULL,
  position INTEGER NOT NULL,
  rising_ns NUMERIC NOT NULL
);

CREATE VIEW qaqc_single_run_table
AS
  SELECT
    ps_boards.id AS psboard_id,
    qaqc_single_run_results.daughterboard_id,
    qaqc_single_run_results.runid AS runid,
    qaqc_runs.run_datetime AS run_timestamp,
    qaqc_runs.shifter,
    qaqc_runs.note AS run_note,
    qaqc_positions.name as position,
    qaqc_single_run_results.qspip,
    qaqc_single_run_results.recov,
    qaqc_single_run_results.power,
    qaqc_single_run_results.clock,
    qaqc_single_run_results.asdtp,
    qaqc_single_run_results.reset,
    qaqc_single_run_results.qaqc_result,
    qaqc_single_run_results.lvds_tx_skew - qaqc_positions.rising_ns AS lvds_tx_skew,
    qaqc_runs.shiftscript_ver,
    qaqc_single_run_results.note AS result_note
  FROM
    ps_boards,
    qaqc_single_run_results,
    qaqc_positions,
    qaqc_runs
  WHERE
    ps_boards.id = qaqc_single_run_results.psboard_id
    AND qaqc_single_run_results.runid = qaqc_runs.id
    AND qaqc_positions.id = qaqc_single_run_results.position
    -- AND ps_boards.id = 356
  ORDER BY
    qaqc_runs.run_datetime ASC,
    qaqc_positions.id;

CREATE VIEW qaqc_extra_run_table
AS
  SELECT
    ps_boards.id AS psboard_id,
    qaqc_runs.id AS runid,
    qaqc_runs.run_datetime AS run_timestamp,
    qaqc_runs.shifter,
    qaqc_runs.note AS run_note,
    qaqc_positions.name as position,
    qaqc_extra_run_results.insufficient_reset_with_10,
    qaqc_extra_run_results.reset_failed_though_reconfig_done,
    qaqc_extra_run_results.always_hit_flag_true,
    qaqc_extra_run_results.dac_is_0,
    qaqc_extra_run_results.efficiency_99percent,
    qaqc_extra_run_results.bcid_shift,
    qaqc_extra_run_results.bcid_fail_000,
    qaqc_extra_run_results.bcid_fail_111,
    qaqc_extra_run_results.low_efficiency,
    qaqc_extra_run_results.bcid_fail,
    qaqc_extra_run_results.power_out_of_range,
    qaqc_extra_run_results.invalid_register_value,
    qaqc_runs.shiftscript_ver,
    qaqc_runs.shifter,
    qaqc_extra_run_results.note AS result_note
  FROM
    ps_boards,
    qaqc_extra_run_results,
    qaqc_positions,
    qaqc_runs
  WHERE
    ps_boards.id = qaqc_extra_run_results.psboard_id
    AND qaqc_extra_run_results.runid = qaqc_runs.id
    AND qaqc_positions.id = qaqc_extra_run_results.position
  ORDER BY
    qaqc_runs.id ASC,
    qaqc_positions.id;

