CREATE TABLE IF NOT EXISTS licenses (
  id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  license_key VARCHAR(80) NOT NULL UNIQUE,
  client_name VARCHAR(160) DEFAULT NULL,
  client_email VARCHAR(180) DEFAULT NULL,
  plan VARCHAR(40) DEFAULT 'mensual',
  status ENUM('active','inactive','blocked') NOT NULL DEFAULT 'active',
  hwid VARCHAR(255) DEFAULT NULL,
  max_devices TINYINT UNSIGNED NOT NULL DEFAULT 1,
  expires_at DATETIME DEFAULT NULL,
  activated_at DATETIME DEFAULT NULL,
  last_check_at DATETIME DEFAULT NULL,
  last_ip VARCHAR(60) DEFAULT NULL,
  notes TEXT DEFAULT NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  INDEX idx_license_key (license_key),
  INDEX idx_status (status),
  INDEX idx_expires_at (expires_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS license_logs (
  id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  license_id BIGINT UNSIGNED DEFAULT NULL,
  event_type VARCHAR(60) NOT NULL,
  message VARCHAR(255) DEFAULT NULL,
  hwid VARCHAR(255) DEFAULT NULL,
  ip_address VARCHAR(60) DEFAULT NULL,
  user_agent VARCHAR(255) DEFAULT NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  INDEX idx_license_id (license_id),
  INDEX idx_event_type (event_type),
  CONSTRAINT fk_license_logs_license
    FOREIGN KEY (license_id) REFERENCES licenses(id)
    ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
