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

// ── Enums ──────────────────────────────────────────────────────────────

export const noteCategoryValidator = v.union(
  v.literal("GENERAL"),
  v.literal("BILLING"),
  v.literal("SUPPORT"),
  v.literal("COMPLIANCE"),
  v.literal("TECHNICAL"),
  v.literal("SALES"),
);

export const integrationTypeValidator = v.union(
  v.literal("whatsapp"),
  v.literal("google_calendar"),
  v.literal("outlook_calendar"),
  v.literal("salesforce"),
  v.literal("hubspot"),
  v.literal("zapier"),
  v.literal("make"),
  v.literal("google_oauth"),
  v.literal("google_contacts"),
);

export const integrationStatusValidator = v.union(
  v.literal("connected"),
  v.literal("disconnected"),
  v.literal("error"),
  v.literal("pending_setup"),
);

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

export const miscTables = {
  company_profiles: defineTable({
    prismaId: v.optional(v.string()),
    user_email: v.string(),
    user_id: v.optional(v.string()),
    company_name: v.string(),
    industry: v.string(),
    company_size: v.string(),
    gcc_country: v.string(),
    phone_number: v.string(),
    address: v.optional(v.string()),
    website: v.optional(v.string()),
    agent_count: v.optional(v.string()),
    agent_use_case: v.optional(v.string()),
    company_description: v.optional(v.string()),
    services: v.array(v.string()),
    selected_plan: v.optional(v.string()),
    billing_period: v.optional(v.string()),
    currency: v.optional(v.string()),
    stripe_session_id: v.optional(v.string()),
    onboarding_complete: v.boolean(),
    verified: v.boolean(),
    created_at: v.number(),
    updated_at: v.number(),
  })
    .index("by_company_size", ["company_size"])
    .index("by_created_at", ["created_at"])
    .index("by_industry", ["industry"])
    .index("by_user_email", ["user_email"]),

  system_settings: defineTable({
    prismaId: v.optional(v.string()),
    key: v.string(),
    value: v.string(),
    encrypted: v.boolean(),
    description: v.optional(v.string()),
    category: v.string(),
    updatedBy: v.optional(v.string()),
    createdAt: v.number(),
    updatedAt: v.number(),
  })
    .index("by_category", ["category"])
    .index("by_key", ["key"]),

  admin_notes: defineTable({
    prismaId: v.optional(v.string()),
    userId: v.string(),
    staffId: v.string(),
    content: v.string(),
    category: noteCategoryValidator,
    isPrivate: v.boolean(),
    createdAt: v.number(),
    updatedAt: v.number(),
  })
    .index("by_category", ["category"])
    .index("by_staffId", ["staffId"])
    .index("by_userId_createdAt", ["userId", "createdAt"]),

  integrations: defineTable({
    prismaId: v.optional(v.string()),
    userId: v.string(),
    type: integrationTypeValidator,
    status: integrationStatusValidator,
    config: v.optional(v.any()),
    accessToken: v.optional(v.string()),
    refreshToken: v.optional(v.string()),
    lastSyncAt: v.optional(v.number()),
    lastError: v.optional(v.string()),
    createdAt: v.number(),
    updatedAt: v.number(),
    scope: v.optional(v.string()),
    tokenExpiry: v.optional(v.number()),
  })
    .index("by_status", ["status"])
    .index("by_type", ["type"])
    .index("by_userId", ["userId"])
    .index("by_userId_type", ["userId", "type"]),
};
