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

const topicValidator = v.union(
  v.literal("products"),
  v.literal("orders"),
  v.literal("support"),
  v.literal("other"),
);

/**
 * Public mutation: submit a contact message. No auth required.
 */
export const submit = mutation({
  args: {
    fullName: v.string(),
    email: v.string(),
    topic: topicValidator,
    message: v.string(),
  },
  handler: async (ctx, args) => {
    const now = Date.now();
    return await ctx.db.insert("messages", {
      fullName: args.fullName,
      email: args.email,
      topic: args.topic,
      message: args.message,
      createdAt: now,
      bookmarked: false,
    });
  },
});
