-- Nay Store Auth / Session V2 optional settings
-- Safe to run multiple times.

CREATE TABLE IF NOT EXISTS api_customer_tokens (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    customer_id INT NOT NULL,
    token_hash CHAR(64) NOT NULL UNIQUE,
    token_label VARCHAR(120) NULL,
    expires_at DATETIME NULL,
    last_used_at DATETIME NULL,
    revoked_at DATETIME NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_customer (customer_id),
    INDEX idx_expires (expires_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

INSERT INTO settings (setting_key, setting_value, setting_group)
SELECT 'mobile_access_token_ttl_days', '180', 'mobile_app'
WHERE NOT EXISTS (SELECT 1 FROM settings WHERE setting_key = 'mobile_access_token_ttl_days');
