import { pgTable, text, timestamp, jsonb, bigint, integer } from "drizzle-orm/pg-core";

// ============================================================================
// Record types stored as JSONB
// ============================================================================

export type TrailRecord = {
  $type: "app.sidetrail.trail";
  title: string;
  description: string;
  stops: Array<{
    tid: string;
    title: string;
    content: string;
    buttonText?: string;
    external?: {
      uri: string;
      title?: string;
      description?: string;
      thumb?: { $type: "blob"; ref: { $link: string }; mimeType: string };
    };
  }>;
  accentColor: string;
  backgroundColor: string;
  createdAt: string;
};

export type WalkRecord = {
  $type: "app.sidetrail.walk";
  trail: { uri: string; cid: string };
  visitedStops: string[];
  createdAt: string;
  updatedAt?: string;
};

export type CompletionRecord = {
  $type: "app.sidetrail.completion";
  trail: { uri: string; cid: string };
  createdAt: string;
};

export type DraftRecord = {
  $type: "app.sidetrail.draft";
  title: string;
  description: string;
  stops: Array<{
    tid: string;
    title: string;
    content: string;
    buttonText?: string;
    external?: {
      uri: string;
      title?: string;
      description?: string;
      thumb?: string; // URL string (not blob ref - uploaded on publish)
    };
  }>;
  accentColor: string;
  backgroundColor: string;
  createdAt: string;
  updatedAt: string;
};

// ============================================================================
// Tables
// ============================================================================

export const trails = pgTable("trails", {
  uri: text("uri").primaryKey(),
  cid: text("cid").notNull(),
  authorDid: text("author_did").notNull(),
  rkey: text("rkey").notNull(),
  record: jsonb("record").notNull().$type<TrailRecord>(),
  createdAt: timestamp("created_at", { withTimezone: true }).notNull(),
  indexedAt: timestamp("indexed_at", { withTimezone: true }).notNull().defaultNow(),
});

export const walks = pgTable("walks", {
  uri: text("uri").primaryKey(),
  cid: text("cid").notNull(),
  authorDid: text("author_did").notNull(),
  rkey: text("rkey").notNull(),
  trailUri: text("trail_uri").notNull(),
  record: jsonb("record").notNull().$type<WalkRecord>(),
  createdAt: timestamp("created_at", { withTimezone: true }).notNull(),
  indexedAt: timestamp("indexed_at", { withTimezone: true }).notNull().defaultNow(),
});

export const completions = pgTable("completions", {
  uri: text("uri").primaryKey(),
  cid: text("cid").notNull(),
  authorDid: text("author_did").notNull(),
  rkey: text("rkey").notNull(),
  trailUri: text("trail_uri").notNull(),
  record: jsonb("record").notNull().$type<CompletionRecord>(),
  createdAt: timestamp("created_at", { withTimezone: true }).notNull(),
  indexedAt: timestamp("indexed_at", { withTimezone: true }).notNull().defaultNow(),
});

export const drafts = pgTable("drafts", {
  id: text("id").primaryKey(), // Composite key: "author_did:rkey"
  authorDid: text("author_did").notNull(),
  rkey: text("rkey").notNull(),
  record: jsonb("record").notNull().$type<DraftRecord>(),
  createdAt: timestamp("created_at", { withTimezone: true }).notNull(),
  updatedAt: timestamp("updated_at", { withTimezone: true }).notNull(),
  version: integer("version").notNull().default(1),
});

export const ingestionCursor = pgTable("ingestion_cursor", {
  id: integer("id").primaryKey().default(1),
  cursorUs: bigint("cursor_us", { mode: "number" }).notNull(),
  updatedAt: timestamp("updated_at", { withTimezone: true }).notNull().defaultNow(),
});

// Account status tracking for takedown/suspension handling
// Status values follow AT Protocol account lifecycle:
// - null: active account (default)
// - 'deactivated': user temporarily paused account (hide content, keep data)
// - 'suspended': host temporarily paused account (hide content, keep data)
// - 'takendown': host took down account (delete content)
// - 'deleted': account deleted (delete content)
export const accounts = pgTable("accounts", {
  did: text("did").primaryKey(),
  active: integer("active").notNull().default(1), // 1 = active, 0 = inactive
  status: text("status"), // null, 'deactivated', 'suspended', 'takendown', 'deleted'
  seq: bigint("seq", { mode: "number" }).notNull(),
  shadowban: integer("shadowban").notNull().default(0), // 1 = hide from homepage
  updatedAt: timestamp("updated_at", { withTimezone: true }).notNull().defaultNow(),
});

// Auth tables
export const authState = pgTable("auth_state", {
  key: text("key").primaryKey(),
  state: text("state").notNull(),
  createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
});

export const authSession = pgTable("auth_session", {
  key: text("key").primaryKey(),
  session: text("session").notNull(),
  createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
  updatedAt: timestamp("updated_at", { withTimezone: true }).notNull().defaultNow(),
});
