/**
 * TypeScript interfaces for Convex document shapes.
 *
 * These mirror the Convex schema tables and are used in API routes to type
 * the return values of convexQuery / convexMutation without relying on `any`.
 *
 * All `_id` fields use `string` rather than `Id<"tableName">` so API routes
 * do not need to import the generated dataModel for simple property access.
 */

// ── Auth domain ─────────────────────────────────────────────────────────

export type UserRole = "CUSTOMER" | "STAFF" | "ADMIN" | "SUPER_ADMIN";

export interface ConvexUser {
  _id: string;
  _creationTime: number;
  prismaId?: string;
  email: string;
  name: string;
  password?: string;
  googleId?: string;
  clerkId?: string;
  emailVerified?: number;
  createdAt: number;
  updatedAt: number;
  lastLoginAt?: number;
  lastLoginIp?: string;
  failedAttempts: number;
  lockedUntil?: number;
  address?: string;
  companyName?: string;
  companySize?: string;
  industry?: string;
  onboardingComplete: boolean;
  phoneNumber?: string;
  website?: string;
  country?: string;
  preferredCurrency?: string;
  city?: string;
  selectedPackage?: string;
  goals?: unknown;
  stripeCustomerId?: string;
  avatar?: string;
  isActive: boolean;
  isSuspended: boolean;
  lastLoginCity?: string;
  lastLoginCountry?: string;
  loginCount: number;
  n8nWebhookUrl?: string;
  n8nWorkflowId?: string;
  role: UserRole;
  suspendedAt?: number;
  suspendedBy?: string;
  suspendedReason?: string;
  username?: string;
  whatsappConnected: boolean;
  whatsappConnectedAt?: number;
  whatsappNumber?: string;
  n8nWebhookToken?: string;
  onboardingStep: number;
  voiceAgentConfig?: unknown;
  voiceAgentId?: string;
}

export interface ConvexUserSession {
  _id: string;
  _creationTime: number;
  userId: string;
  token: string;
  deviceName: string;
  deviceType: string;
  ipAddress: string;
  location?: string;
  country?: string;
  city?: string;
  userAgent: string;
  lastActive: number;
  createdAt: number;
  expiresAt: number;
  loginHistoryId?: string;
}

export interface ConvexUserPreferences {
  _id: string;
  _creationTime: number;
  userId: string;
  language: string;
  timezone: string;
  dateFormat: string;
  theme: string;
  emailNotifications: unknown;
  smsNotifications: unknown;
  whatsappNotifications: unknown;
  emailFrequency: string;
  dashboardPreferences: unknown;
  createdAt: number;
  updatedAt: number;
}

export interface ConvexAuditLog {
  _id: string;
  _creationTime: number;
  userId?: string;
  type: string;
  action: string;
  success: boolean;
  ip: string;
  userAgent?: string;
  metadata?: Record<string, unknown>;
  createdAt: number;
}

export interface ConvexOnboardingProgress {
  _id: string;
  _creationTime: number;
  userId: string;
  welcomeCompleted: boolean;
  whatsappCompleted: boolean;
  whatsappMethod?: string;
  voiceAgentCompleted: boolean;
  knowledgeBaseCompleted: boolean;
  workflowsCompleted: boolean;
  testingCompleted: boolean;
  startedAt: number;
  completedAt?: number;
  lastStepAt: number;
  dismissedAt?: number;
  dismissCount: number;
}

// ── Organization domain ─────────────────────────────────────────────────

export interface ConvexOrganizationMembership {
  _id: string;
  _creationTime: number;
  userId: string;
  organizationId: string;
  role: string;
}

export interface ConvexOrganization {
  _id: string;
  _creationTime: number;
  ownerId: string;
  name: string;
}

// ── Billing domain ──────────────────────────────────────────────────────

export interface ConvexSubscription {
  _id: string;
  _creationTime: number;
  userId: string;
  tierId?: string;
  status: string;
  billingPeriod?: string;
  currentPeriodEnd?: number;
  trialEndsAt?: number;
  setupFeePaid?: boolean;
  setupFeeAmount?: number;
  setupFeePaidAt?: number;
  cardBrand?: string;
  cardLastFour?: string;
  paymentMethodId?: string;
  stripeCustomerId?: string;
  stripeSubscriptionId?: string;
  cancelAtPeriodEnd?: boolean;
  cancelledAt?: number;
  lastPaymentDate?: number;
  paymentFailureCount?: number;
}

// ── Customers domain ────────────────────────────────────────────────────

export interface ConvexCustomer {
  _id: string;
  _creationTime: number;
  userId: string;
  name: string;
  phone: string;
  email?: string;
  notes?: string;
}

// ── User links domain ───────────────────────────────────────────────────

export interface ConvexUserLink {
  _id: string;
  _creationTime: number;
  userId: string;
  linkType: string;
  title: string;
  url: string;
  description?: string;
  icon?: string;
  sortOrder: number;
}
