-- Create database and table Werte_BZG for health data tracking

CREATE DATABASE IF NOT EXISTS RXF CHARACTER SET utf8 COLLATE utf8_general_ci;

USE RXF;

CREATE TABLE IF NOT EXISTS Werte_BZG (
  ID INT AUTO_INCREMENT PRIMARY KEY,
  Datum DATE NOT NULL,
  Zeit TIME NOT NULL,
  Zucker INT NULL COMMENT 'Blood sugar in mg/dl',
  Essen TEXT NULL COMMENT 'Meal information',
  Gewicht DECIMAL(5,1) NULL COMMENT 'Weight in kg',
  DruckS INT NULL COMMENT 'Systolic blood pressure in mmHg',
  DruckD INT NULL COMMENT 'Diastolic blood pressure in mmHg',
  Puls INT NULL COMMENT 'Pulse rate',
  INDEX idx_datum_zeit (Datum, Zeit)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

-- WebAuthn-Passkeys je Benutzer (Benutzer stammen aus AUTH_USERS).
-- Die Anwendung legt diese Tabelle beim ersten Zugriff selbst an (lib/passkeys.ts),
-- sofern der DB-Benutzer CREATE-Rechte hat.
CREATE TABLE IF NOT EXISTS passkeys (
  credential_id VARCHAR(255) CHARACTER SET ascii COLLATE ascii_bin NOT NULL PRIMARY KEY,
  username VARCHAR(64) NOT NULL,
  public_key VARBINARY(1024) NOT NULL,
  counter INT UNSIGNED NOT NULL DEFAULT 0,
  transports VARCHAR(255) NOT NULL DEFAULT '[]',
  label VARCHAR(80) NOT NULL DEFAULT '',
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  last_used_at DATETIME NULL,
  INDEX idx_passkeys_username (username)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
