-- Datenbank: sternwarte
-- Die Tabelle 'beos' ist bereits vorhanden und wird hier nicht neu angelegt.

CREATE TABLE objekte (
  ID INT AUTO_INCREMENT PRIMARY KEY,
  Name VARCHAR(200) NOT NULL UNIQUE,
  LastUsed TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE logbuch (
  ID INT AUTO_INCREMENT PRIMARY KEY,
  Kuppel ENUM('West','Ost','Süd','Pluto') NOT NULL DEFAULT 'West',
  ArtFuehrung ENUM('RF','SF','BEOS','SonF','TD','Beob','ToT','Sonst') NOT NULL DEFAULT 'RF',
  Beginn DATETIME NOT NULL,
  Ende DATETIME NOT NULL,
  Besucher INT DEFAULT 0,
  Bemerkungen TEXT,
  WetterTemp DECIMAL(5,1),
  WetterFeuchte DECIMAL(5,1),
  WetterDruck DECIMAL(7,1),
  created_by INT,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (created_by) REFERENCES beos(id)
);

CREATE TABLE logbuch_beos (
  ID INT AUTO_INCREMENT PRIMARY KEY,
  LogbuchID INT NOT NULL,
  BeoID INT NOT NULL,
  FOREIGN KEY (LogbuchID) REFERENCES logbuch(ID) ON DELETE CASCADE,
  FOREIGN KEY (BeoID) REFERENCES beos(id)
);

CREATE TABLE logbuch_objekte (
  ID INT AUTO_INCREMENT PRIMARY KEY,
  LogbuchID INT NOT NULL,
  ObjektID INT,
  ObjektName VARCHAR(200) NOT NULL,
  FOREIGN KEY (LogbuchID) REFERENCES logbuch(ID) ON DELETE CASCADE,
  FOREIGN KEY (ObjektID) REFERENCES objekte(ID)
);
