/**
 * report_shortcuts schema — reports-navigation-hub (wave-13).
 * Postgres / Neon via Hyperdrive.
 *
 * Table:
 *  - report_shortcuts: per-user saved report view shortcuts with params
 *
 * DB conventions:
 * - UUID PK .defaultRandom()
 * - TIMESTAMPTZ via timestamp(col, { withTimezone: true })
 * - Enums: text() + check(... IN (...)) — NEVER pgEnum
 * - Plain btree indexes via drizzle index()
 */
import { pgTable, uuid, text, jsonb, timestamp, index, check } from 'drizzle-orm/pg-core'
import { sql } from 'drizzle-orm'
import { tenants } from './tenants'
import { users } from './users'

export const reportShortcuts = pgTable(
  'report_shortcuts',
  {
    id: uuid('id').primaryKey().defaultRandom(),
    tenantId: uuid('tenant_id')
      .notNull()
      .references(() => tenants.id, { onDelete: 'cascade' }),
    userId: uuid('user_id')
      .notNull()
      .references(() => users.id, { onDelete: 'cascade' }),
    name: text('name').notNull(),
    reportType: text('report_type').notNull(),
    params: jsonb('params').notNull(),
    createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
  },
  (t) => ({
    userIdx: index('idx_report_shortcuts_user').on(t.tenantId, t.userId, t.createdAt.desc()),
    reportTypeCheck: check(
      'report_shortcuts_report_type_check',
      sql`${t.reportType} IN (
        'revenue','invoices','payments','time','expenses',
        'profitability','revenue_forecast','leads','proposals',
        'ar_aging','bad_debt','audit','api_usage',
        'vat','pnl','cashflow','advance_tax','withholding',
        'bituach_leumi','uniform_format'
      )`,
    ),
  }),
)

export type ReportShortcutRow = typeof reportShortcuts.$inferSelect
export type NewReportShortcut = typeof reportShortcuts.$inferInsert
