-- Рабочие места (агенты Windows)
CREATE TABLE IF NOT EXISTS workstations (
  id               UUID        PRIMARY KEY DEFAULT gen_random_uuid(),
  hotel_id         UUID        NOT NULL REFERENCES hotels(id) ON DELETE CASCADE,
  agent_id         VARCHAR(64) UNIQUE,          -- UUID генерируемый агентом
  name             VARCHAR(100) NOT NULL,        -- «Ресепшн 1», «Бар»
  hostname         VARCHAR(255),                 -- имя Windows-компьютера
  ip_address       INET,
  is_online        BOOLEAN     NOT NULL DEFAULT false,
  last_seen        TIMESTAMPTZ,
  created_at       TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

-- Коды сопряжения (6 цифр, TTL 10 минут)
CREATE TABLE IF NOT EXISTS agent_pair_codes (
  code             CHAR(6)     PRIMARY KEY,
  workstation_id   UUID        NOT NULL REFERENCES workstations(id) ON DELETE CASCADE,
  expires_at       TIMESTAMPTZ NOT NULL,
  used             BOOLEAN     NOT NULL DEFAULT false
);

-- Устройства привязанные к рабочему месту
CREATE TABLE IF NOT EXISTS workstation_devices (
  id               UUID        PRIMARY KEY DEFAULT gen_random_uuid(),
  workstation_id   UUID        NOT NULL REFERENCES workstations(id) ON DELETE CASCADE,
  type             VARCHAR(20) NOT NULL CHECK (type IN ('kkt', 'printer')),
  name             VARCHAR(100) NOT NULL,
  connection       VARCHAR(20) NOT NULL CHECK (connection IN ('usb', 'network')),
  network_host     VARCHAR(255),
  network_port     INTEGER,
  purpose          VARCHAR(30) CHECK (purpose IN ('fiscal', 'kitchen', 'bar', 'receipt', 'other')),
  config           JSONB       NOT NULL DEFAULT '{}'
);
