CREATE TABLE IF NOT EXISTS `Solvent` (
  `solvent_id` TEXT NOT NULL UNIQUE,
  `name` text,
  PRIMARY KEY (`solvent_id`)
);

CREATE TABLE IF NOT EXISTS `Preprep_vial` (
  `vial_id` TEXT NOT NULL UNIQUE,
  `name` text,
  `type` text,
  PRIMARY KEY (`vial_id`)
);

CREATE TABLE IF NOT EXISTS `Catalyst` (
  `catalyst_id` TEXT NOT NULL UNIQUE,
  `name` text,
  PRIMARY KEY (`catalyst_id`)
);

CREATE TABLE IF NOT EXISTS `Vial_Catalyst` (
  `vial_id` TEXT,
  `catalyst_id` TEXT,
  `vial_catalyst_id` TEXT NOT NULL UNIQUE,
  `name` text,
  `type` text,
  PRIMARY KEY (`vial_catalyst_id`),
  FOREIGN KEY (`vial_id`)
      REFERENCES `Preprep_vial`(`vial_id`),
  FOREIGN KEY (`catalyst_id`)
      REFERENCES `Catalyst`(`catalyst_id`)
);

CREATE TABLE IF NOT EXISTS `Incubator` (
  `incubator_id` TEXT NOT NULL UNIQUE,
  `type` text,
  `name` text,
  PRIMARY KEY (`incubator_id`)
);

CREATE TABLE IF NOT EXISTS `Organization` (
  `org_id` TEXT NOT NULL UNIQUE,
  `name` text,
  `address` text,
  `contact_info` text,
  PRIMARY KEY (`org_id`)
);

CREATE TABLE IF NOT EXISTS `Project` (
  `org_id` TEXT,
  `project_id` TEXT NOT NULL UNIQUE,
  `mdr_id` text,
  `name` text,
  `description` text,
  `Start Date` date,
  PRIMARY KEY (`project_id`),
  FOREIGN KEY (`org_id`)
      REFERENCES `Organization`(`org_id`)
);

CREATE TABLE IF NOT EXISTS `Generation_Run` (
  `project_id` TEXT,
  `run_id` TEXT NOT NULL UNIQUE,
  `repository` text,
  `rounds` int,
  `status` text,
  `run_start` Timestamp,
  `run_end` Timestamp,
  `compute_consumed` float,
  `wandb_link` text,
  PRIMARY KEY (`run_id`),
  FOREIGN KEY (`project_id`)
      REFERENCES `Project`(`project_id`)
);

CREATE TABLE IF NOT EXISTS `Candidate` (
  `run_id` TEXT,
  `candidate_id` TEXT NOT NULL UNIQUE,
  `project_id` TEXT,
  `name` text,
  `smiles_id` text,
  `estimated_cost` float,
  `generated_time` Timestamp,
  `synthesis_approved` Timestamp,
  `status` text,
  PRIMARY KEY (`candidate_id`),
  FOREIGN KEY (`run_id`)
      REFERENCES `Generation_Run`(`run_id`)
);

CREATE TABLE IF NOT EXISTS `Candidate_Synthesis_Plan` (
  `candidate_id` TEXT,
  `plan_id` TEXT NOT NULL UNIQUE,
  `conditions_doc_link` text,
  PRIMARY KEY (`plan_id`),
  FOREIGN KEY (`candidate_id`)
      REFERENCES `Candidate`(`candidate_id`)
);

CREATE TABLE IF NOT EXISTS `Laboratory` (
  `lab_id` TEXT NOT NULL UNIQUE,
  `name` text,
  `description` text,
  `location` text,
  PRIMARY KEY (`lab_id`)
);

CREATE TABLE IF NOT EXISTS `Well_Plate` (
  `plan_id` TEXT,
  `lab_id` TEXT,
  `well_plate_id` TEXT NOT NULL UNIQUE,
  `incubation_start` Timestamp,
  `incubation_end` Timestamp,
  `Temperature` float,
  `incubator_id` TEXT,
  `status` text,
  PRIMARY KEY (`well_plate_id`),
  FOREIGN KEY (`plan_id`)
      REFERENCES `Candidate_Synthesis_Plan`(`plan_id`),
  FOREIGN KEY (`lab_id`)
      REFERENCES `Laboratory`(`lab_id`)
);

CREATE TABLE IF NOT EXISTS `Sample_Well` (
  `well_plate_id` TEXT,
  `sample_id` TEXT NOT NULL UNIQUE,
  `picture_url` text,
  `name` text,
  PRIMARY KEY (`sample_id`),
  FOREIGN KEY (`well_plate_id`)
      REFERENCES `Well_Plate`(`well_plate_id`)
);

CREATE TABLE IF NOT EXISTS `Well_Solution` (
  `sample_id` TEXT,
  `vial_id` TEXT,
  `well_solution_id` TEXT NOT NULL UNIQUE,
  `units` text,
  `amount` Float,
  PRIMARY KEY (`well_solution_id`),
  FOREIGN KEY (`vial_id`)
      REFERENCES `Preprep_vial`(`vial_id`),
  FOREIGN KEY (`sample_id`)
      REFERENCES `Sample_Well`(`sample_id`)
);

CREATE TABLE IF NOT EXISTS `Well_Catalysts` (
  `sample_id` TEXT,
  `catalyst_id` TEXT,
  `well_catalyst_id` TEXT NOT NULL UNIQUE,
  `units` text,
  `amount` Float,
  PRIMARY KEY (`well_catalyst_id`),
  FOREIGN KEY (`catalyst_id`)
      REFERENCES `Catalyst`(`catalyst_id`),
  FOREIGN KEY (`sample_id`)
      REFERENCES `Sample_Well`(`sample_id`)
);

CREATE TABLE IF NOT EXISTS `MDR` (
  `mdr_id` TEXT NOT NULL UNIQUE,
  `name` text,
  `description` text,
  `date_approved` date,
  PRIMARY KEY (`mdr_id`)
);

CREATE TABLE IF NOT EXISTS `Well_Solvents` (
  `sample_id` TEXT,
  `solvent_id` TEXT,
  `well_solvent_id` TEXT NOT NULL UNIQUE,
  `units` text,
  `amount` Float,
  PRIMARY KEY (`well_solvent_id`),
  FOREIGN KEY (`solvent_id`)
      REFERENCES `Solvent`(`solvent_id`),
  FOREIGN KEY (`sample_id`)
      REFERENCES `Sample_Well`(`sample_id`)
);

CREATE TABLE IF NOT EXISTS `MDR_Goals` (
  `goal_id` TEXT NOT NULL UNIQUE,
  `mdr_id` TEXT,
  `goal_name` text,
  `operator` text,
  `measurement` float,
  PRIMARY KEY (`goal_id`),
  FOREIGN KEY (`mdr_id`)
      REFERENCES `MDR`(`mdr_id`)
);

CREATE TABLE IF NOT EXISTS `Vial_Solvent` (
  `vial_id` TEXT,
  `solvent_id` TEXT,
  `vial_solvent_id` TEXT NOT NULL UNIQUE,
  `units` text,
  `amount` Float,
  PRIMARY KEY (`vial_solvent_id`),
  FOREIGN KEY (`solvent_id`)
      REFERENCES `Solvent`(`solvent_id`),
  FOREIGN KEY (`vial_id`)
      REFERENCES `Preprep_vial`(`vial_id`)
);

CREATE TABLE IF NOT EXISTS `Solid` (
  `solid_id` TEXT NOT NULL UNIQUE,
  `name` text,
  `smiles_id` text,
  PRIMARY KEY (`solid_id`)
);

CREATE TABLE IF NOT EXISTS `Well_Solids` (
  `sample_id` TEXT,
  `solid_id` TEXT,
  `well_solid_id` TEXT NOT NULL UNIQUE,
  `units` text,
  `amount` Float,
  PRIMARY KEY (`well_solid_id`),
  FOREIGN KEY (`solid_id`)
      REFERENCES `Solid`(`solid_id`),
  FOREIGN KEY (`sample_id`)
      REFERENCES `Sample_Well`(`sample_id`)
);

CREATE TABLE IF NOT EXISTS `Users` (
  `user_id` TEXT NOT NULL UNIQUE,
  `org_id` TEXT,
  `username` text,
  `password` text,
  `role` text,
  PRIMARY KEY (`user_id`),
  FOREIGN KEY (`org_id`)
      REFERENCES `Organization`(`org_id`)
);

CREATE TABLE IF NOT EXISTS `TGA_Analysis` (
  `sample_id` TEXT,
  `tga_id` TEXT NOT NULL UNIQUE,
  `temp_comp_array` Int,
  `date` Timestamp,
  PRIMARY KEY (`tga_id`),
  FOREIGN KEY (`sample_id`)
      REFERENCES `Sample_Well`(`sample_id`)
);

CREATE TABLE IF NOT EXISTS `tga_scorecard` (
  `tag_id` TEXT,
  `tga_score_id` TEXT NOT NULL UNIQUE,
  `hashtable` Int,
  PRIMARY KEY (`tga_score_id`),
  FOREIGN KEY (`tag_id`)
      REFERENCES `TGA_Analysis`(`tga_id`)
);

CREATE TABLE IF NOT EXISTS `PXRD_Characterization` (
  `sample_id` TEXT,
  `character_id` TEXT NOT NULL UNIQUE,
  `point_array` Int,
  `pxrd_score` float,
  `date` Timestamp,
  PRIMARY KEY (`character_id`),
  FOREIGN KEY (`sample_id`)
      REFERENCES `Sample_Well`(`sample_id`)
);

CREATE TABLE IF NOT EXISTS `Sample_Post_Instrumentation_Analysis` (
  `sample_id` TEXT,
  `analysis_id` TEXT NOT NULL UNIQUE,
  `summary` text,
  `last_updated` Timestamp,
  PRIMARY KEY (`analysis_id`),
  FOREIGN KEY (`sample_id`)
      REFERENCES `Sample_Well`(`sample_id`)
);

CREATE TABLE IF NOT EXISTS `Candidate_Assessment` (
  `candidate_id` TEXT,
  `assessment_id` TEXT NOT NULL UNIQUE,
  `assessment` text,
  `assessed_by` text,
  `assess_date` Timestamp,
  `status` text,
  PRIMARY KEY (`assessment_id`),
  FOREIGN KEY (`candidate_id`)
      REFERENCES `Candidate`(`candidate_id`)
);

CREATE TABLE IF NOT EXISTS `pxrd_scorecard` (
  `character_id` TEXT,
  `pxrd_score_id` TEXT NOT NULL UNIQUE,
  `hashtable` text,
  PRIMARY KEY (`pxrd_score_id`),
  FOREIGN KEY (`character_id`)
      REFERENCES `PXRD_Characterization`(`character_id`)
);