-- =========================================================
-- Database Sistem Lisensi Sederhana
-- Import file ini lewat phpMyAdmin / mysql CLI
-- =========================================================

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

-- Tabel admin (untuk login ke panel admin)
CREATE TABLE IF NOT EXISTS admins (
    id INT AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(50) NOT NULL UNIQUE,
    password VARCHAR(255) NOT NULL,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB;

-- Tabel lisensi
CREATE TABLE IF NOT EXISTS licenses (
    id INT AUTO_INCREMENT PRIMARY KEY,
    license_key VARCHAR(64) NOT NULL UNIQUE,
    customer_name VARCHAR(150) DEFAULT NULL,
    domain VARCHAR(255) NOT NULL,           -- domain/source web yang diizinkan pakai lisensi ini
    status ENUM('inactive','active','disabled') NOT NULL DEFAULT 'inactive',
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    activated_at DATETIME DEFAULT NULL,
    expires_at DATE DEFAULT NULL,           -- NULL = tidak ada batas waktu (lifetime)
    last_check DATETIME DEFAULT NULL,       -- terakhir kali lisensi diverifikasi lewat API
    last_check_ip VARCHAR(45) DEFAULT NULL,
    notes TEXT DEFAULT NULL
) ENGINE=InnoDB;

-- Log setiap pengecekan lisensi (opsional, buat audit/debug)
CREATE TABLE IF NOT EXISTS license_logs (
    id INT AUTO_INCREMENT PRIMARY KEY,
    license_key VARCHAR(64) NOT NULL,
    domain_request VARCHAR(255) DEFAULT NULL,
    ip_address VARCHAR(45) DEFAULT NULL,
    result ENUM('valid','invalid') NOT NULL,
    message VARCHAR(255) DEFAULT NULL,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB;
