-- Create Database
CREATE DATABASE license_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

USE license_db;

-- Create licenses table
CREATE TABLE licenses (
  id INT AUTO_INCREMENT PRIMARY KEY,
  user_id VARCHAR(255) NOT NULL,
  hashed_key VARCHAR(255) NOT NULL,
  license_type ENUM('one-time', 'subscription', 'trial') DEFAULT 'one-time',
  tier VARCHAR(50) DEFAULT 'basic',
  valid_until DATETIME DEFAULT NULL,
  created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  INDEX idx_user_id (user_id)
);

-- User with Read-Only Access (Client-side validation)
CREATE USER 'license_reader'@'%' IDENTIFIED BY 'secure_read_password';
GRANT SELECT ON license_db.licenses TO 'license_reader'@'%';

-- User with Read-Write Access (Server-side key generation and updating)
CREATE USER 'license_admin'@'%' IDENTIFIED BY 'secure_admin_password';
GRANT SELECT, INSERT, UPDATE ON license_db.licenses TO 'license_admin'@'%';

-- Apply privileges
FLUSH PRIVILEGES;
