import { query } from "../_generated/server";
import { v } from "convex/values";

/**
 * Get a company profile by user email.
 * Used to auto-detect industry for AI question generation.
 */
export const getCompanyProfileByEmail = query({
  args: { email: v.string() },
  handler: async (ctx, { email }) => {
    return ctx.db
      .query("company_profiles")
      .withIndex("by_user_email", (q) => q.eq("user_email", email))
      .first();
  },
});

/**
 * Get a company profile by user_id (Clerk/auth user ID).
 * Used for org-scoped lookups where the effective owner's email is unknown.
 */
export const getCompanyProfileByUserId = query({
  args: { userId: v.string() },
  handler: async (ctx, { userId }) => {
    // user_id field stores the Clerk/auth user ID
    return ctx.db
      .query("company_profiles")
      .filter((q) => q.eq(q.field("user_id"), userId))
      .first();
  },
});
