/**
 * Contractor Time Portal schema — contractor-portal (wave 8).
 *
 * Tables:
 *   contractor_portal_sessions — SHA-256 hashes of magic-link tokens with expiry/used-at
 */
import { pgTable, uuid, text, timestamp, index } from 'drizzle-orm/pg-core'
import { contractors } from './contractors'

export const contractorPortalSessions = pgTable(
  'contractor_portal_sessions',
  {
    id: uuid('id').primaryKey().defaultRandom(),
    contractorId: uuid('contractor_id')
      .notNull()
      .references(() => contractors.id, { onDelete: 'cascade' }),
    tokenHash: text('token_hash').notNull(),
    expiresAt: timestamp('expires_at', { withTimezone: true }).notNull(),
    usedAt: timestamp('used_at', { withTimezone: true }),
    createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
  },
  (t) => ({
    tokenIdx: index('idx_cps_token').on(t.tokenHash),
  }),
)

export type ContractorPortalSession = typeof contractorPortalSessions.$inferSelect
