BEGIN TRANSACTION;
CREATE TABLE IF NOT EXISTS "Configurations" (
	"configuration_id"	INTEGER,
	"run_id"	INTEGER,
	"unique_elab_id"	TEXT,
	"bitrate"	REAL,
	"symbolrate"	REAL,
	"pam_level"	INTEGER,
	"db_mode"	TEXT,
	"pulsef_alpha"	INTEGER,
	"v_bias"	REAL,
	"v_awg"	REAL,
	"precomp_amp"	REAL,
	"rop_attenuation"	REAL,
	"wavelength"	REAL,
	"laser_power"	REAL,
	"fiber_length"	REAL,
	"pd_in_desired"	REAL,
	"is_mpi"	BIT,
	"signal_attenuation"	REAL,
	"interference_path_length"	REAL,
	"interference_attenuation"	REAL,
	"pam_source"	TEXT,
	PRIMARY KEY("configuration_id" AUTOINCREMENT),
	FOREIGN KEY("run_id") REFERENCES "Runs"("run_id")
);
CREATE TABLE IF NOT EXISTS "EqualizerParameters" (
	"eq_id"	INTEGER,
	"equalizer_structure"	REAL,
	"M"	INTEGER,
	"target_constellation"	TEXT,
	"db_target"	INTEGER,
	"diff_precode"	INTEGER,
	"postFFE"	INTEGER,
	"NpostFFE"	INTEGER,
	"Ne1"	INTEGER,
	"Ne2"	INTEGER,
	"Ne3"	INTEGER,
	"Nb1"	INTEGER,
	"Nb2"	INTEGER,
	"Nb3"	INTEGER,
	"K"	INTEGER,
	"DCmu"	REAL,
	"ideal_dfe"	INTEGER,
	"training_length"	INTEGER,
	"training_loops"	INTEGER,
	"TRmu1"	REAL,
	"TRmu2"	REAL,
	"TRmu3"	REAL,
	"TRmuDFE"	REAL,
	"dd_loops"	INTEGER,
	"DDmu1"	REAL,
	"DDmu2"	REAL,
	"DDmu3"	REAL,
	"DDmuDFE"	REAL,
	"MLSE_mode"	TEXT,
	"MLSE_trellis_states"	TEXT,
	"comment"	TEXT,
	"config_hash"	TEXT,
	UNIQUE("config_hash"),
	PRIMARY KEY("eq_id" AUTOINCREMENT)
);
CREATE TABLE IF NOT EXISTS "Measurements" (
	"measurement_id"	INTEGER,
	"run_id"	INTEGER,
	"power_laser"	REAL,
	"power_rop"	REAL,
	"power_pd_in"	REAL,
	"power_mpi_interference"	REAL,
	"power_mpi_signal"	REAL,
	"voa_class"	TEXT,
	"pdfa_class"	TEXT,
	"laser_class"	TEXT,
	PRIMARY KEY("measurement_id" AUTOINCREMENT),
	FOREIGN KEY("run_id") REFERENCES "Runs"("run_id")
);
CREATE TABLE IF NOT EXISTS "Results" (
	"result_id"	INTEGER,
	"run_id"	INTEGER,
	"eqParam_id"	INTEGER,
	"date_of_processing"	DATETIME DEFAULT (datetime('now', 'localtime')),
	"numBits"	INTEGER,
	"numBitErr"	INTEGER,
	"BER"	REAL,
	"numBitErr_precoded"	REAL,
	"BER_precoded"	REAL,
	"SNR"	REAL,
	"SNR_level"	TEXT,
	"GMI"	REAL,
	"AIR"	REAL,
	"EVM"	REAL,
	"EVM_level"	TEXT,
	"Alpha"	REAL,
	"result_hash"	TEXT UNIQUE,
	"MLSE_dir"	INTEGER,
	PRIMARY KEY("result_id" AUTOINCREMENT),
	FOREIGN KEY("eqParam_id") REFERENCES "EqualizerParameters"("eq_id"),
	FOREIGN KEY("run_id") REFERENCES "Runs"("run_id")
);
CREATE TABLE IF NOT EXISTS "Runs" (
	"run_id"	INTEGER,
	"date_of_run"	DATETIME DEFAULT (datetime('now', 'localtime')),
	"tx_bits_path"	TEXT,
	"tx_symbols_path"	TEXT,
	"rx_sync_path"	TEXT,
	"rx_raw_path"	TEXT,
	"filename"	TEXT,
	"tx_signal_path"	TEXT,
	PRIMARY KEY("run_id" AUTOINCREMENT)
);
CREATE VIEW "View_ResultOverview" AS
SELECT
    -- Run info
    Runs.run_id,
    Runs.date_of_run,
	
    Results.BER,
    Results.SNR,
    Results.GMI,
    Results.AIR,
    Results.EVM,
    Results.Alpha,
	
    -- Configurations
    Configurations.symbolrate,
    Configurations.pam_level,
    Configurations.db_mode,
    Configurations.pulsef_alpha,
    Configurations.v_bias,
    Configurations.v_awg,
    Configurations.precomp_amp,
    Configurations.is_mpi,
	Configurations.signal_attenuation,
    Configurations.interference_path_length,
    Configurations.interference_attenuation,

    -- Measurement data
    Measurements.power_laser,
    Measurements.power_rop,
    Measurements.power_pd_in,
    Measurements.power_mpi_interference,
    Measurements.power_mpi_signal,

    -- Equalizer parameters
    EqualizerParameters.equalizer_structure,
    EqualizerParameters.db_target,
    EqualizerParameters.diff_precode


FROM Results

-- Join related tables
LEFT JOIN Runs ON Results.run_id = Runs.run_id
LEFT JOIN Configurations ON Configurations.run_id = Runs.run_id
LEFT JOIN Measurements ON Measurements.run_id = Runs.run_id
LEFT JOIN EqualizerParameters ON Results.eqParam_id = EqualizerParameters.eq_id;
CREATE INDEX IF NOT EXISTS "idx_run_id_on_Configurations" ON "Configurations" (
	"run_id"
);
CREATE INDEX IF NOT EXISTS "idx_run_id_on_Measurements" ON "Measurements" (
	"run_id"
);
COMMIT;
