CREATE DATABASE IF NOT EXISTS `DATABASE_NAME` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
USE `DATABASE_NAME`;

-- Hosts registry for outsidethebox.top (and other zones)
CREATE TABLE IF NOT EXISTS hosts (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,

  -- DNS grouping
  zone VARCHAR(255) NOT NULL,          -- e.g. etica-stats.org
  sub VARCHAR(255) NULL,              -- e.g. explorer (NULL/empty for apex)
  fqdn VARCHAR(512) NOT NULL,         -- e.g. explorer.etica-stats.org (unique)

  -- Status / monitoring
  monitor_enabled TINYINT(1) NOT NULL DEFAULT 0,
  status_code_http INT NULL,
  status_code_https INT NULL,
  status ENUM('up','down','unknown') NOT NULL DEFAULT 'unknown',
  last_check_at DATETIME NULL,
  last_error TEXT NULL,

  -- SSL
  ssl_expires_at DATETIME NULL,

  -- Host/service expiry (your "hostname expires")
  host_expires_at DATETIME NULL,

  -- Infra placement
  public_ip VARCHAR(64) NULL,
  private_ip VARCHAR(64) NULL,
  pve_host VARCHAR(128) NULL,

  -- Business fields
  client_name VARCHAR(255) NULL,
  email VARCHAR(255) NULL,
  country VARCHAR(128) NULL,
  package_type VARCHAR(128) NULL,
  dns_provider VARCHAR(128) NULL,
  notes TEXT NULL,

  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,

  UNIQUE KEY uq_hosts_fqdn (fqdn),
  KEY idx_zone_sub (zone, sub),
  KEY idx_client (client_name)
);

