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

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

export const jobStatusValidator = v.union(
  v.literal("DRAFT"),
  v.literal("OPEN"),
  v.literal("PAUSED"),
  v.literal("CLOSED"),
  v.literal("ARCHIVED"),
);

export const interviewStatusValidator = v.union(
  v.literal("PENDING"),
  v.literal("CONFIRMED"),
  v.literal("RESCHEDULED"),
  v.literal("CANCELLED"),
  v.literal("COMPLETED"),
);

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

export const careerTables = {
  careers: defineTable({
    prismaId: v.optional(v.string()),
    slug: v.string(),
    titleTranslations: v.any(),
    heroLeadTranslations: v.any(),
    heroDescriptionTranslations: v.any(),
    aboutCompany: v.optional(v.any()),
    howWeWork: v.optional(v.any()),
    howToApply: v.optional(v.any()),
    createdAt: v.number(),
    updatedAt: v.number(),
  }).index("by_slug", ["slug"]),

  jobs: defineTable({
    prismaId: v.optional(v.string()),
    careerId: v.optional(v.string()),
    slug: v.string(),
    titleTranslations: v.any(),
    department: v.string(),
    departmentLabel: v.any(),
    locationTranslations: v.any(),
    contractType: v.string(),
    contractTypeLabel: v.any(),
    level: v.string(),
    levelLabel: v.any(),
    compensationTranslations: v.any(),
    benefits: v.optional(v.any()),
    skills: v.optional(v.any()),
    requirements: v.optional(v.any()),
    responsibilities: v.any(),
    niceToHave: v.optional(v.any()),
    workdayExpectations: v.optional(v.any()),
    applicationInstructions: v.optional(v.any()),
    status: jobStatusValidator,
    openingAt: v.number(),
    closingAt: v.optional(v.number()),
    createdAt: v.number(),
    updatedAt: v.number(),
  })
    .index("by_careerId", ["careerId"])
    .index("by_department", ["department"])
    .index("by_openingAt", ["openingAt"])
    .index("by_status", ["status"])
    .index("by_status_openingAt_closingAt", [
      "status",
      "openingAt",
      "closingAt",
    ])
    .index("by_slug", ["slug"]),

  job_applications: defineTable({
    prismaId: v.optional(v.string()),
    jobId: v.optional(v.string()),
    applicantName: v.string(),
    applicantEmail: v.string(),
    applicantPhone: v.string(),
    applicantLinkedin: v.optional(v.string()),
    applicantPortfolio: v.optional(v.string()),
    coverNote: v.optional(v.string()),
    cvBucket: v.string(),
    cvPath: v.string(),
    cvFilename: v.string(),
    cvBytes: v.number(),
    cvMimeType: v.string(),
    submissionLanguage: v.string(),
    submittedAt: v.number(),
    metadata: v.optional(v.any()),
    createdAt: v.number(),
    hiredAt: v.optional(v.number()),
    internalNotes: v.optional(v.string()),
    interviewLink: v.optional(v.string()),
    interviewScheduledAt: v.optional(v.number()),
    rejectionReason: v.optional(v.string()),
    reviewedAt: v.optional(v.number()),
    reviewedBy: v.optional(v.string()),
    status: v.optional(v.string()),
    statusUpdatedAt: v.optional(v.number()),
  })
    .index("by_applicantEmail", ["applicantEmail"])
    .index("by_jobId", ["jobId"])
    .index("by_reviewedBy", ["reviewedBy"])
    .index("by_statusUpdatedAt", ["statusUpdatedAt"])
    .index("by_status", ["status"])
    .index("by_submittedAt", ["submittedAt"]),

  application_notes: defineTable({
    prismaId: v.optional(v.string()),
    applicationId: v.string(),
    staffId: v.string(),
    staffName: v.string(),
    noteText: v.string(),
    createdAt: v.number(),
    updatedAt: v.number(),
  })
    .index("by_applicationId", ["applicationId"])
    .index("by_createdAt", ["createdAt"])
    .index("by_staffId", ["staffId"]),

  interview_bookings: defineTable({
    prismaId: v.optional(v.string()),
    applicationId: v.string(),
    jobId: v.optional(v.string()),
    tokenId: v.optional(v.string()),
    applicantName: v.string(),
    applicantEmail: v.string(),
    applicantPhone: v.optional(v.string()),
    preferredDate: v.number(),
    preferredTime: v.number(),
    timezone: v.string(),
    durationMinutes: v.number(),
    confirmedDate: v.optional(v.number()),
    confirmedTime: v.optional(v.number()),
    meetingLink: v.optional(v.string()),
    meetingPlatform: v.optional(v.string()),
    meetingId: v.optional(v.string()),
    meetingPassword: v.optional(v.string()),
    status: interviewStatusValidator,
    applicantNotes: v.optional(v.string()),
    staffNotes: v.optional(v.string()),
    assignedTo: v.optional(v.string()),
    confirmedAt: v.optional(v.number()),
    confirmedBy: v.optional(v.string()),
    cancelledAt: v.optional(v.number()),
    cancelledBy: v.optional(v.string()),
    cancellationReason: v.optional(v.string()),
    createdAt: v.number(),
    updatedAt: v.number(),
  })
    .index("by_applicantEmail", ["applicantEmail"])
    .index("by_applicationId", ["applicationId"])
    .index("by_assignedTo", ["assignedTo"])
    .index("by_confirmedAt", ["confirmedAt"])
    .index("by_createdAt", ["createdAt"])
    .index("by_jobId", ["jobId"])
    .index("by_preferredDate", ["preferredDate"])
    .index("by_status", ["status"])
    .index("by_status_preferredDate", ["status", "preferredDate"])
    .index("by_tokenId", ["tokenId"]),

  interview_booking_tokens: defineTable({
    prismaId: v.optional(v.string()),
    token: v.string(),
    applicationId: v.string(),
    jobId: v.optional(v.string()),
    applicantEmail: v.string(),
    expiresAt: v.number(),
    usedAt: v.optional(v.number()),
    isRevoked: v.boolean(),
    createdAt: v.number(),
    updatedAt: v.number(),
  })
    .index("by_applicantEmail", ["applicantEmail"])
    .index("by_applicationId", ["applicationId"])
    .index("by_expiresAt", ["expiresAt"])
    .index("by_isRevoked", ["isRevoked"])
    .index("by_token", ["token"])
    .index("by_usedAt", ["usedAt"]),
};
