CREATE TABLE IF NOT EXISTS licenses (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  license_key VARCHAR(100) NOT NULL,
  customer_name VARCHAR(120) DEFAULT NULL,
  status ENUM('active','suspended','expired','revoked') NOT NULL DEFAULT 'active',
  expires_at DATETIME DEFAULT NULL,
  max_installs INT NOT NULL DEFAULT 1,
  notes TEXT DEFAULT NULL,
  created_at DATETIME NOT NULL,
  updated_at DATETIME NOT NULL,
  PRIMARY KEY (id),
  UNIQUE KEY uq_license_key (license_key)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS license_installs (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  license_id BIGINT UNSIGNED NOT NULL,
  install_id VARCHAR(100) NOT NULL,
  client_fingerprint VARCHAR(255) DEFAULT NULL,
  user_agent VARCHAR(255) DEFAULT NULL,
  status ENUM('active','disabled') NOT NULL DEFAULT 'active',
  last_seen_at DATETIME DEFAULT NULL,
  created_at DATETIME NOT NULL,
  updated_at DATETIME NOT NULL,
  PRIMARY KEY (id),
  UNIQUE KEY uq_install_id (install_id),
  KEY idx_license_id (license_id),
  CONSTRAINT fk_license_installs_license
    FOREIGN KEY (license_id) REFERENCES licenses(id)
    ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

INSERT INTO licenses
(license_key, customer_name, status, expires_at, max_installs, notes, created_at, updated_at)
VALUES
('DEMO-CHARLES-2026', 'Cliente Demo', 'active', DATE_ADD(UTC_TIMESTAMP(), INTERVAL 365 DAY), 1, 'Licencia de prueba', UTC_TIMESTAMP(), UTC_TIMESTAMP());
