import { defineTable } from "convex/server";
import { v } from "convex/values";

// ── Validators ────────────────────────────────────────────────────────

export const storageBucketValidator = v.union(
  v.literal("contact-attachments"),
  v.literal("job-applications"),
  v.literal("blog-images"),
  v.literal("avatars"),
  v.literal("email-attachments"),
);

// ── Tables ────────────────────────────────────────────────────────────

export const storageTables = {
  file_uploads: defineTable({
    storageId: v.id("_storage"),
    fileName: v.string(),
    fileType: v.string(),
    fileSize: v.number(),
    bucket: v.string(), // one of storageBucketValidator values
    uploadedBy: v.optional(v.string()),
    entityId: v.optional(v.string()), // e.g., contact submission ID, job application ID
    entityType: v.optional(v.string()), // e.g., "contact_submission", "job_application"
    createdAt: v.number(),
  })
    .index("by_bucket", ["bucket"])
    .index("by_entityType_entityId", ["entityType", "entityId"])
    .index("by_uploadedBy", ["uploadedBy"])
    .index("by_storageId", ["storageId"]),
};
