/**
 * User repository.
 *
 * Provides typed CRUD operations for the `users` table.
 */
import { getConnection } from '../../../modules/database/connection.js';
const normalizePublicUser = (row) => {
    if (!row) {
        return undefined;
    }
    return {
        id: row.id,
        username: row.username,
        created_at: row.created_at,
        last_login: row.last_login,
        isAdmin: row.is_admin === 1,
    };
};
export const userDb = {
    /** Returns true if at least one user exists in the database. */
    hasUsers() {
        const db = getConnection();
        const row = db.prepare('SELECT COUNT(*) as count FROM users').get();
        return row.count > 0;
    },
    /** Inserts a new user and returns the created public fields. */
    createUser(username, passwordHash, options = {}) {
        const db = getConnection();
        const isAdmin = options.isAdmin ?? false;
        const result = db
            .prepare('INSERT INTO users (username, password_hash, is_admin) VALUES (?, ?, ?)')
            .run(username, passwordHash, isAdmin ? 1 : 0);
        return {
            id: Number(result.lastInsertRowid),
            username,
            isAdmin,
        };
    },
    /** Looks up an active user by username, including password hash for auth verification. */
    getUserByUsername(username) {
        const db = getConnection();
        return db
            .prepare('SELECT * FROM users WHERE username = ? AND is_active = 1')
            .get(username);
    },
    /** Updates the last_login timestamp. Non-fatal — logs but does not throw. */
    updateLastLogin(userId) {
        try {
            const db = getConnection();
            db.prepare('UPDATE users SET last_login = CURRENT_TIMESTAMP WHERE id = ?').run(userId);
        }
        catch (err) {
            const message = err instanceof Error ? err.message : String(err);
            console.error('Failed to update last login', { error: message });
        }
    },
    /** Returns public user fields by ID (no password hash). */
    getUserById(userId) {
        const db = getConnection();
        const row = db
            .prepare('SELECT id, username, created_at, last_login, is_admin FROM users WHERE id = ? AND is_active = 1')
            .get(userId);
        return normalizePublicUser(row);
    },
    /** Returns the first active user. Used for single-user mode lookups. */
    getFirstUser() {
        const db = getConnection();
        const row = db
            .prepare('SELECT id, username, created_at, last_login, is_admin FROM users WHERE is_active = 1 ORDER BY id ASC LIMIT 1')
            .get();
        return normalizePublicUser(row);
    },
    listUsers() {
        const db = getConnection();
        const rows = db
            .prepare('SELECT id, username, created_at, last_login, is_admin FROM users WHERE is_active = 1 ORDER BY id ASC')
            .all();
        return rows.map((row) => normalizePublicUser(row)).filter(Boolean);
    },
    deleteUser(userId) {
        const db = getConnection();
        const result = db.prepare('DELETE FROM users WHERE id = ?').run(userId);
        return result.changes > 0;
    },
    /** Stores the user's preferred git name and email. */
    updateGitConfig(userId, gitName, gitEmail) {
        const db = getConnection();
        db.prepare('UPDATE users SET git_name = ?, git_email = ? WHERE id = ?').run(gitName, gitEmail, userId);
    },
    /** Retrieves the user's git identity (name + email). */
    getGitConfig(userId) {
        const db = getConnection();
        return db
            .prepare('SELECT git_name, git_email FROM users WHERE id = ?')
            .get(userId);
    },
    /** Marks onboarding as complete for the given user. */
    completeOnboarding(userId) {
        const db = getConnection();
        db.prepare('UPDATE users SET has_completed_onboarding = 1 WHERE id = ?').run(userId);
    },
    /** Returns true if the user has finished the onboarding flow. */
    hasCompletedOnboarding(userId) {
        const db = getConnection();
        const row = db
            .prepare('SELECT has_completed_onboarding FROM users WHERE id = ?')
            .get(userId);
        return row?.has_completed_onboarding === 1;
    },
    /** Updates the password hash for a given user. */
    updatePassword(userId, passwordHash) {
        const db = getConnection();
        db.prepare('UPDATE users SET password_hash = ? WHERE id = ?').run(passwordHash, userId);
    },
};
//# sourceMappingURL=users.js.map