CREATE TABLE beos (
  ID INT AUTO_INCREMENT PRIMARY KEY,
  Kuerzel VARCHAR(10) NOT NULL UNIQUE,
  Name VARCHAR(100) NOT NULL,
  Passwort VARCHAR(255),
  MustChangePassword TINYINT(1) DEFAULT 1,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

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('Reguläre Führung','Sonderführung','BEO-Sitzung','Sonnenführung','Technischer Dienst','Beobachtung','Tag der offenen Tür','Sonstiges') NOT NULL DEFAULT 'Reguläre Führung',
  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)
);
