CREATE TABLE IF NOT EXISTS users (
  id INT AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(255) NULL,
  username VARCHAR(100) NOT NULL,
  email VARCHAR(255) NULL,
  password_hash VARCHAR(255) NOT NULL,
  role VARCHAR(50) NULL,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  UNIQUE KEY uq_users_username (username)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS devices (
  id INT AUTO_INCREMENT PRIMARY KEY,
  owner_key VARCHAR(255) NOT NULL,
  owner_name VARCHAR(255) NULL,
  location VARCHAR(255) NOT NULL,
  host VARCHAR(255) NOT NULL,
  port VARCHAR(20) NOT NULL,
  username VARCHAR(255) NULL,
  password VARCHAR(255) NULL,
  prefix VARCHAR(255) NOT NULL,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  INDEX idx_devices_owner_key (owner_key)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

ALTER TABLE users ADD COLUMN IF NOT EXISTS role VARCHAR(50) NULL AFTER password_hash;
ALTER TABLE devices ADD COLUMN IF NOT EXISTS owner_key VARCHAR(255) NOT NULL AFTER id;
ALTER TABLE devices ADD COLUMN IF NOT EXISTS owner_name VARCHAR(255) NULL AFTER owner_key;
ALTER TABLE devices ADD COLUMN IF NOT EXISTS username VARCHAR(255) NULL AFTER port;
ALTER TABLE devices ADD COLUMN IF NOT EXISTS password VARCHAR(255) NULL AFTER username;
ALTER TABLE devices ADD COLUMN IF NOT EXISTS prefix VARCHAR(255) NOT NULL AFTER password;


-- Doplnkové poznámky pre admin rozhranie
-- Stránka admin-settings.html načítava tabuľky users a devices priamo cez backend API.
-- Zmeny vykonané na stránke sa zapisujú do databázy enviro okamžite cez UPDATE / INSERT / DELETE.
-- Rozhranie obnovuje tabuľky každé 2 sekundy, aby bolo vidno zmeny takmer v reálnom čase.
