import { index, integer, sqliteTable, text, uniqueIndex } from "drizzle-orm/sqlite-core";

export const brands = sqliteTable("brands", {
  id: text("id").primaryKey(), ownerId: text("owner_id").notNull(), name: text("name").notNull(),
  description: text("description").notNull().default(""), color: text("color").notNull().default("#7657e8"),
  createdAt: integer("created_at").notNull(), updatedAt: integer("updated_at").notNull(),
}, (table) => [index("idx_brands_owner").on(table.ownerId)]);

export const socialAccounts = sqliteTable("social_accounts", {
  id: text("id").primaryKey(), ownerId: text("owner_id").notNull(),
  brandId: text("brand_id").notNull().references(() => brands.id, { onDelete: "cascade" }),
  platform: text("platform", { enum: ["instagram", "threads"] }).notNull(),
  platformUserId: text("platform_user_id").notNull(), username: text("username").notNull(),
  profileImageUrl: text("profile_image_url"), tokenEncrypted: text("token_encrypted").notNull(),
  tokenIv: text("token_iv").notNull(), tokenExpiresAt: integer("token_expires_at"),
  status: text("status", { enum: ["connected", "expired", "error"] }).notNull().default("connected"),
  createdAt: integer("created_at").notNull(), updatedAt: integer("updated_at").notNull(),
}, (table) => [
  index("idx_social_accounts_brand").on(table.ownerId, table.brandId),
  uniqueIndex("idx_social_accounts_brand_platform_user").on(table.brandId, table.platform, table.platformUserId),
]);

export const scheduledPosts = sqliteTable("scheduled_posts", {
  id: text("id").primaryKey(), ownerId: text("owner_id").notNull(),
  brandId: text("brand_id").notNull().references(() => brands.id, { onDelete: "cascade" }),
  content: text("content").notNull(), mediaUrl: text("media_url"),
  mediaType: text("media_type", { enum: ["image", "video"] }), altText: text("alt_text"),
  scheduledFor: integer("scheduled_for").notNull(), timezone: text("timezone").notNull().default("Asia/Seoul"),
  status: text("status", { enum: ["scheduled", "processing", "published", "failed", "cancelled"] }).notNull().default("scheduled"),
  publishedAt: integer("published_at"), errorMessage: text("error_message"),
  createdAt: integer("created_at").notNull(), updatedAt: integer("updated_at").notNull(),
}, (table) => [
  index("idx_scheduled_posts_owner_brand").on(table.ownerId, table.brandId),
  index("idx_scheduled_posts_due").on(table.status, table.scheduledFor),
]);

export const scheduledPostTargets = sqliteTable("scheduled_post_targets", {
  id: text("id").primaryKey(), postId: text("post_id").notNull().references(() => scheduledPosts.id, { onDelete: "cascade" }),
  accountId: text("account_id").references(() => socialAccounts.id, { onDelete: "set null" }),
  platform: text("platform", { enum: ["instagram", "threads"] }).notNull(),
  status: text("status", { enum: ["scheduled", "processing", "published", "failed", "cancelled"] }).notNull().default("scheduled"),
  externalPostId: text("external_post_id"), errorMessage: text("error_message"), publishedAt: integer("published_at"),
  createdAt: integer("created_at").notNull(), updatedAt: integer("updated_at").notNull(),
}, (table) => [index("idx_post_targets_post").on(table.postId)]);

export const oauthStates = sqliteTable("oauth_states", {
  id: text("id").primaryKey(), ownerId: text("owner_id").notNull(),
  brandId: text("brand_id").notNull().references(() => brands.id, { onDelete: "cascade" }),
  platform: text("platform", { enum: ["instagram", "threads"] }).notNull(),
  expiresAt: integer("expires_at").notNull(), createdAt: integer("created_at").notNull(),
}, (table) => [index("idx_oauth_states_expiry").on(table.expiresAt)]);
