-- Database: generous_dbcihuy

-- Tabel config untuk menyimpan konfigurasi
CREATE TABLE IF NOT EXISTS config (
    id INT AUTO_INCREMENT PRIMARY KEY,
    k VARCHAR(50) UNIQUE NOT NULL,
    v TEXT NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Insert default config values
INSERT INTO config (k, v) VALUES 
('site_name', '.Generous Key System'),
('key_duration_hours', '24'),
('safelink_wait_secs', '15')
ON DUPLICATE KEY UPDATE v = v;

-- Tabel keys_data untuk menyimpan key
CREATE TABLE IF NOT EXISTS keys_data (
    id INT AUTO_INCREMENT PRIMARY KEY,
    key_str VARCHAR(50) UNIQUE NOT NULL,
    created_at INT NOT NULL,
    expires_at INT NOT NULL,
    note TEXT,
    active BOOLEAN DEFAULT 1,
    INDEX idx_key (key_str),
    INDEX idx_expires (expires_at)
);

-- Tabel safelink_tokens untuk tracking token
CREATE TABLE IF NOT EXISTS safelink_tokens (
    id INT AUTO_INCREMENT PRIMARY KEY,
    token VARCHAR(100) UNIQUE NOT NULL,
    created_at INT NOT NULL,
    used BOOLEAN DEFAULT 0,
    INDEX idx_token (token),
    INDEX idx_created (created_at)
);