import { eq } from 'drizzle-orm';
import { db } from './client';
import { users, type User } from './schema';

export function getUserByEmail(email: string): User | undefined {
  const normalized = email.trim().toLowerCase();
  return db.select().from(users).where(eq(users.email, normalized)).get();
}

export function touchUserLastLogin(userId: string): void {
  db.update(users).set({ lastLoginAt: new Date() }).where(eq(users.id, userId)).run();
}

export function setUserPasswordHash(userId: string, passwordHash: string): void {
  db.update(users).set({ passwordHash }).where(eq(users.id, userId)).run();
}
