import { pgTable, serial, integer, text, boolean, timestamp, jsonb, uniqueIndex } from 'drizzle-orm/pg-core';
import { users } from '../auth/schema';

export const emailAccounts = pgTable(
  'email_accounts',
  {
    id: serial('id').primaryKey(),
    userId: integer('user_id')
      .notNull()
      .references(() => users.id, { onDelete: 'cascade' }),
    provider: text('provider').notNull(),
    email: text('email').notNull(),
    displayName: text('display_name'),
    imapHost: text('imap_host').notNull(),
    imapPort: integer('imap_port').notNull(),
    imapSecure: boolean('imap_secure').notNull().default(true),
    authType: text('auth_type').notNull(),
    credentials: jsonb('credentials').notNull().default({}),
    enabled: boolean('enabled').notNull().default(true),
    createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
    status: text('status').notNull().default('connected'),
    /**
     * LEGACY, read-only. The sync position now lives in the account's own `emails.db`
     * (`sidecar/email/sync-meta.ts`), so the file is self-describing and an account can be moved to
     * another machine by copying it.
     *
     * Kept because it is the one-time backfill source for every account created before that move; the
     * first sync after it migrates these keys into SQLite and nothing writes here again. Dropping it
     * would strand any account that has not synced since.
     */
    syncMeta: jsonb('sync_meta').notNull().default({}),
    updatedAt: timestamp('updated_at', { withTimezone: true }).notNull().defaultNow(),
  },
  (table) => [uniqueIndex('uq_email_accounts_user_email').on(table.userId, table.email)],
);
