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

export default defineSchema({
  userProfiles: defineTable({
    username: v.string(),
    clientSalt: v.string(),
    encryptedMasterKey: v.string(),
    hashedAuthKey: v.string(),
    publicIdentityKey: v.string(),
    publicSigningKey: v.string(),
    encryptedPrivateKeys: v.string(),
    isAdmin: v.boolean(),
    status: v.optional(v.string()),
    displayName: v.optional(v.string()),
    avatarStorageId: v.optional(v.id("_storage")),
    aboutMe: v.optional(v.string()),
    customStatus: v.optional(v.string()),
    joinSoundStorageId: v.optional(v.id("_storage")),
    accentColor: v.optional(v.string()),
  }).index("by_username", ["username"]),

  categories: defineTable({
    name: v.string(),
    position: v.number(),
  }).index("by_position", ["position"]),

  channels: defineTable({
    name: v.string(),
    type: v.string(), // 'text' | 'voice' | 'dm'
    categoryId: v.optional(v.id("categories")),
    topic: v.optional(v.string()),
    position: v.optional(v.number()),
  }).index("by_name", ["name"])
    .index("by_category", ["categoryId"]),

  messages: defineTable({
    channelId: v.id("channels"),
    senderId: v.id("userProfiles"),
    ciphertext: v.string(),
    nonce: v.string(),
    signature: v.string(),
    keyVersion: v.number(),
    replyTo: v.optional(v.id("messages")),
    editedAt: v.optional(v.number()),
    pinned: v.optional(v.boolean()),
  }).index("by_channel", ["channelId"])
    .index("by_channel_pinned", ["channelId", "pinned"])
    .index("by_sender", ["senderId"]),

  messageReactions: defineTable({
    messageId: v.id("messages"),
    userId: v.id("userProfiles"),
    emoji: v.string(),
  })
    .index("by_message", ["messageId"])
    .index("by_message_user_emoji", ["messageId", "userId", "emoji"])
    .index("by_user", ["userId"]),

  channelKeys: defineTable({
    channelId: v.id("channels"),
    userId: v.id("userProfiles"),
    encryptedKeyBundle: v.string(),
    keyVersion: v.number(),
  })
    .index("by_channel", ["channelId"])
    .index("by_user", ["userId"])
    .index("by_channel_and_user", ["channelId", "userId"]),

  roles: defineTable({
    name: v.string(),
    color: v.string(),
    position: v.number(),
    permissions: v.any(), // JSON object of permissions
    isHoist: v.boolean(),
  }),

  userRoles: defineTable({
    userId: v.id("userProfiles"),
    roleId: v.id("roles"),
  })
    .index("by_user", ["userId"])
    .index("by_role", ["roleId"])
    .index("by_user_and_role", ["userId", "roleId"]),

  invites: defineTable({
    code: v.string(),
    encryptedPayload: v.string(),
    createdBy: v.id("userProfiles"),
    maxUses: v.optional(v.number()),
    uses: v.number(),
    expiresAt: v.optional(v.number()), // timestamp
    keyVersion: v.number(),
  }).index("by_code", ["code"])
    .index("by_creator", ["createdBy"]),

  dmParticipants: defineTable({
    channelId: v.id("channels"),
    userId: v.id("userProfiles"),
  })
    .index("by_channel", ["channelId"])
    .index("by_user", ["userId"]),

  typingIndicators: defineTable({
    channelId: v.id("channels"),
    userId: v.id("userProfiles"),
    username: v.string(),
    expiresAt: v.number(), // timestamp
  }).index("by_channel", ["channelId"])
    .index("by_user", ["userId"]),

  voiceStates: defineTable({
    channelId: v.id("channels"),
    userId: v.id("userProfiles"),
    username: v.string(),
    isMuted: v.boolean(),
    isDeafened: v.boolean(),
    isScreenSharing: v.boolean(),
    isServerMuted: v.boolean(),
    watchingStream: v.optional(v.id("userProfiles")),
    lastHeartbeat: v.optional(v.number()),
  })
    .index("by_channel", ["channelId"])
    .index("by_user", ["userId"]),

  channelReadState: defineTable({
    userId: v.id("userProfiles"),
    channelId: v.id("channels"),
    lastReadTimestamp: v.number(),
  })
    .index("by_user", ["userId"])
    .index("by_channel", ["channelId"])
    .index("by_user_and_channel", ["userId", "channelId"]),

  serverSettings: defineTable({
    serverName: v.optional(v.string()),
    afkChannelId: v.optional(v.id("channels")),
    afkTimeout: v.number(), // seconds (default 300 = 5 min)
    iconStorageId: v.optional(v.id("_storage")),
  }),

  customEmojis: defineTable({
    name: v.string(),
    storageId: v.id("_storage"),
    uploadedBy: v.id("userProfiles"),
    // `true` for animated (GIF / APNG) uploads so the settings UI
    // can split Static vs Animated in separate sections. Optional
    // so existing rows without the flag still validate — they
    // surface as static in the UI by default.
    animated: v.optional(v.boolean()),

    createdAt: v.number(),
  }).index("by_name", ["name"])
    .index("by_uploader", ["uploadedBy"]),

  polls: defineTable({
    channelId: v.id("channels"),
    createdBy: v.id("userProfiles"),
    question: v.string(),
    options: v.array(
      v.object({
        id: v.string(),
        text: v.string(),
      }),
    ),
    allowMultiple: v.boolean(),
    disclosed: v.boolean(),
    closed: v.boolean(),
    closesAt: v.optional(v.number()),
    createdAt: v.number(),
  })
    .index("by_channel", ["channelId"])
    .index("by_creator", ["createdBy"]),

  pollVotes: defineTable({
    pollId: v.id("polls"),
    userId: v.id("userProfiles"),
    optionIds: v.array(v.string()),
    votedAt: v.number(),
  })
    .index("by_poll", ["pollId"])
    .index("by_poll_and_user", ["pollId", "userId"]),

  pollReactions: defineTable({
    pollId: v.id("polls"),
    userId: v.id("userProfiles"),
    emoji: v.string(),
  })
    .index("by_poll", ["pollId"])
    .index("by_poll_user_emoji", ["pollId", "userId", "emoji"])
    .index("by_user", ["userId"]),

  savedMedia: defineTable({
    userId: v.id("userProfiles"),
    // Convex storage URL — also the dedupe key for a single user.
    url: v.string(),
    kind: v.string(), // 'image' | 'video' | 'audio'
    filename: v.string(),
    mimeType: v.optional(v.string()),
    width: v.optional(v.number()),
    height: v.optional(v.number()),
    size: v.optional(v.number()),
    // Re-post path: keep the per-file AES key + iv so the same
    // attachment metadata can be embedded in a future message
    // without re-uploading. The file stays encrypted in storage —
    // saving just bookmarks the metadata.
    encryptionKey: v.string(),
    encryptionIv: v.string(),
    savedAt: v.number(),
  })
    .index("by_user", ["userId"])
    .index("by_user_and_url", ["userId", "url"]),
});
