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

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

export const socialPlatformValidator = v.union(
  v.literal("INSTAGRAM"),
  v.literal("FACEBOOK"),
  v.literal("TIKTOK"),
  v.literal("TWITTER"),
);

export const socialIntegrationStatusValidator = v.union(
  v.literal("CONNECTED"),
  v.literal("DISCONNECTED"),
  v.literal("TOKEN_EXPIRED"),
  v.literal("ERROR"),
);

export const socialQueueStatusValidator = v.union(
  v.literal("DRAFT"),
  v.literal("SCHEDULED"),
  v.literal("PUBLISHING"),
  v.literal("PUBLISHED"),
  v.literal("FAILED"),
);

export const socialPostStatusValidator = v.union(
  v.literal("DRAFT"),
  v.literal("SCHEDULED"),
  v.literal("PUBLISHED"),
  v.literal("FAILED"),
);

export const aiContentTypeValidator = v.union(
  v.literal("BLOG"),
  v.literal("SOCIAL_POST"),
  v.literal("IMAGE"),
);

export const socialMessageDirectionValidator = v.union(
  v.literal("INBOUND"),
  v.literal("OUTBOUND"),
);

export const socialTrendTypeValidator = v.union(
  v.literal("HASHTAG"),
  v.literal("TOPIC"),
  v.literal("ENGAGEMENT_PATTERN"),
  v.literal("BEST_TIME"),
);

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

export const socialTables = {
  social_accounts: defineTable({
    prismaId: v.optional(v.string()),
    userId: v.string(),
    platform: socialPlatformValidator,
    platformAccountId: v.string(),
    accountName: v.string(),
    accessToken: v.string(),
    refreshToken: v.optional(v.string()),
    tokenExpiry: v.optional(v.number()),
    status: socialIntegrationStatusValidator,
    metadata: v.optional(v.any()),
    createdAt: v.number(),
    updatedAt: v.number(),
  })
    .index("by_userId", ["userId"])
    .index("by_userId_platform", ["userId", "platform"])
    .index("by_status", ["status"])
    .index("by_userId_platform_platformAccountId", [
      "userId",
      "platform",
      "platformAccountId",
    ]),

  social_content_queue: defineTable({
    prismaId: v.optional(v.string()),
    blogPostId: v.optional(v.string()),
    socialAccountId: v.string(),
    platform: socialPlatformValidator,
    content: v.string(),
    hashtags: v.array(v.string()),
    mediaUrls: v.array(v.string()),
    status: socialQueueStatusValidator,
    scheduledAt: v.optional(v.number()),
    publishedAt: v.optional(v.number()),
    platformPostId: v.optional(v.string()),
    platformUrl: v.optional(v.string()),
    retryCount: v.number(),
    errorMessage: v.optional(v.string()),
    createdAt: v.number(),
    updatedAt: v.number(),
    calendarColor: v.optional(v.string()),
    campaignTag: v.optional(v.string()),
  })
    .index("by_socialAccountId", ["socialAccountId"])
    .index("by_status", ["status"])
    .index("by_scheduledAt", ["scheduledAt"])
    .index("by_platform", ["platform"])
    .index("by_blogPostId", ["blogPostId"])
    .index("by_campaignTag", ["campaignTag"]),

  social_analytics: defineTable({
    prismaId: v.optional(v.string()),
    queueItemId: v.string(),
    impressions: v.number(),
    likes: v.number(),
    comments: v.number(),
    shares: v.number(),
    clicks: v.number(),
    fetchedAt: v.number(),
    createdAt: v.number(),
    updatedAt: v.number(),
    reach: v.number(),
    engagementRate: v.number(),
    saves: v.number(),
  })
    .index("by_queueItemId", ["queueItemId"])
    .index("by_fetchedAt", ["fetchedAt"]),

  social_messages: defineTable({
    prismaId: v.optional(v.string()),
    socialAccountId: v.string(),
    platform: socialPlatformValidator,
    direction: socialMessageDirectionValidator,
    conversationId: v.string(),
    externalMessageId: v.optional(v.string()),
    senderName: v.optional(v.string()),
    senderHandle: v.optional(v.string()),
    senderAvatarUrl: v.optional(v.string()),
    content: v.string(),
    mediaUrls: v.array(v.string()),
    isRead: v.boolean(),
    threadId: v.optional(v.string()),
    parentMessageId: v.optional(v.string()),
    metadata: v.optional(v.any()),
    createdAt: v.number(),
    updatedAt: v.number(),
  })
    .index("by_socialAccountId", ["socialAccountId"])
    .index("by_conversationId", ["conversationId"])
    .index("by_platform_conversationId", ["platform", "conversationId"])
    .index("by_socialAccountId_isRead", ["socialAccountId", "isRead"])
    .index("by_createdAt", ["createdAt"])
    .index("by_threadId", ["threadId"]),

  social_trends: defineTable({
    prismaId: v.optional(v.string()),
    userId: v.string(),
    platform: v.optional(socialPlatformValidator),
    trendType: socialTrendTypeValidator,
    title: v.string(),
    description: v.optional(v.string()),
    score: v.number(),
    hashtags: v.array(v.string()),
    metadata: v.optional(v.any()),
    expiresAt: v.number(),
    createdAt: v.number(),
    updatedAt: v.number(),
  })
    .index("by_userId", ["userId"])
    .index("by_userId_trendType", ["userId", "trendType"])
    .index("by_expiresAt", ["expiresAt"])
    .index("by_platform", ["platform"]),

  social_posts: defineTable({
    prismaId: v.optional(v.string()),
    status: socialPostStatusValidator,
    platforms: v.array(v.string()),
    content: v.string(),
    hashtags: v.array(v.string()),
    link: v.optional(v.string()),
    mediaIds: v.array(v.string()),
    lang: v.string(),
    scheduledAt: v.optional(v.number()),
    publishedAt: v.optional(v.number()),
    campaignId: v.optional(v.string()),
    audienceTags: v.array(v.string()),
    errors: v.optional(v.any()),
    createdBy: v.optional(v.string()),
    updatedBy: v.optional(v.string()),
    createdAt: v.number(),
    updatedAt: v.number(),
  })
    .index("by_campaignId", ["campaignId"])
    .index("by_scheduledAt", ["scheduledAt"])
    .index("by_status", ["status"]),

  ai_content_generations: defineTable({
    prismaId: v.optional(v.string()),
    contentType: aiContentTypeValidator,
    inputPrompt: v.string(),
    outputContent: v.string(),
    model: v.string(),
    tokens: v.optional(v.number()),
    createdBy: v.string(),
    createdAt: v.number(),
  })
    .index("by_contentType", ["contentType"])
    .index("by_createdBy", ["createdBy"])
    .index("by_createdAt", ["createdAt"]),
};
