// src/lib/account/credential.ts — l'utilisateur a-t-il un mot de passe ?
// Un signup magic link / social ne crée AUCUN compte credential : le formulaire
// « changer le mot de passe » (qui exige l'actuel) est alors une impasse. Même
// règle que le setPassword de better-auth : providerId "credential" AVEC hash.
import "server-only";

import { and, eq, isNotNull } from "drizzle-orm";
import { account } from "@/lib/db/auth-schema";
import { db } from "@/lib/db/client";

export async function hasCredentialAccount(userId: string): Promise<boolean> {
  const rows = await db
    .select({ id: account.id })
    .from(account)
    .where(
      and(
        eq(account.userId, userId),
        eq(account.providerId, "credential"),
        isNotNull(account.password),
      ),
    )
    .limit(1);
  return rows.length > 0;
}
