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

CREATE TABLE IF NOT EXISTS licenses (
  id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  license_key VARCHAR(64) NOT NULL UNIQUE,
  customer_name VARCHAR(150) NOT NULL,
  customer_email VARCHAR(190) DEFAULT NULL,
  product VARCHAR(100) NOT NULL DEFAULT 'Muara Kasir Pro',
  status ENUM('active','inactive','expired','blocked') NOT NULL DEFAULT 'active',
  device_id VARCHAR(128) DEFAULT NULL,
  device_name VARCHAR(190) DEFAULT NULL,
  activated_at DATETIME DEFAULT NULL,
  expired_at DATETIME DEFAULT NULL,
  last_check_at DATETIME DEFAULT NULL,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  INDEX idx_device_id (device_id),
  INDEX idx_status (status),
  INDEX idx_expired_at (expired_at)
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS activation_logs (
  id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  license_id BIGINT UNSIGNED DEFAULT NULL,
  license_key VARCHAR(64) DEFAULT NULL,
  device_id VARCHAR(128) DEFAULT NULL,
  action VARCHAR(50) NOT NULL,
  ip_address VARCHAR(45) DEFAULT NULL,
  user_agent TEXT DEFAULT NULL,
  message VARCHAR(255) DEFAULT NULL,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  INDEX idx_license_id (license_id),
  INDEX idx_created_at (created_at),
  CONSTRAINT fk_activation_license
    FOREIGN KEY (license_id) REFERENCES licenses(id)
    ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB;
