import { query } from '../db.js';
import { hashKey } from '../keys.js';
export async function licenseKeyAuth(c, next) {
    const key = c.req.header('X-License-Key');
    if (!key) {
        return c.json({ error: 'Missing X-License-Key header' }, 401);
    }
    const keyHash = hashKey(key);
    const result = query(`SELECT id, user_id, key_hash, key_prefix, balance_cents, status, tier, created_at
     FROM license_keys WHERE key_hash = ? AND status = 'active'`, [keyHash]);
    if (result.rows.length === 0) {
        return c.json({ error: 'Invalid or revoked license key' }, 401);
    }
    const licenseKey = result.rows[0];
    c.set('licenseKeyId', licenseKey.id);
    c.set('licenseKeyUserId', licenseKey.user_id);
    c.set('licenseKeyBalance', licenseKey.balance_cents);
    c.set('licenseKeyTier', licenseKey.tier);
    c.set('licenseKeyHash', keyHash);
    await next();
}
//# sourceMappingURL=license-key.js.map