import { pgTable, text, timestamp, index, uniqueIndex } from 'drizzle-orm/pg-core'; import { sql } from 'drizzle-orm'; import { ports } from './ports'; import { berths } from './berths'; import { clients } from './clients'; import { yachts } from './yachts'; import { interests } from './interests'; import { files } from './documents'; export const berthReservations = pgTable( 'berth_reservations', { id: text('id') .primaryKey() .$defaultFn(() => crypto.randomUUID()), berthId: text('berth_id') .notNull() .references(() => berths.id), portId: text('port_id') .notNull() .references(() => ports.id), clientId: text('client_id') .notNull() .references(() => clients.id), yachtId: text('yacht_id') .notNull() .references(() => yachts.id), interestId: text('interest_id').references(() => interests.id), status: text('status').notNull(), // 'pending' | 'active' | 'ended' | 'cancelled' startDate: timestamp('start_date', { withTimezone: true, mode: 'date' }).notNull(), endDate: timestamp('end_date', { withTimezone: true, mode: 'date' }), tenureType: text('tenure_type').notNull().default('permanent'), // 'permanent' | 'fixed_term' | 'seasonal' contractFileId: text('contract_file_id').references(() => files.id), notes: text('notes'), createdBy: text('created_by').notNull(), createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(), updatedAt: timestamp('updated_at', { withTimezone: true }).notNull().defaultNow(), }, (table) => [ index('idx_br_berth').on(table.berthId), index('idx_br_client').on(table.clientId), index('idx_br_yacht').on(table.yachtId), index('idx_br_port').on(table.portId), // Cover the FKs Postgres doesn't auto-index. Without these, deleting // (or restrict-checking) the parent interest / contract file row // requires a full scan of berth_reservations. (idx_br_interest is // already used by berth_recommendations — namespace this one.) index('idx_brr_interest').on(table.interestId), index('idx_brr_contract_file').on(table.contractFileId), uniqueIndex('idx_br_active') .on(table.berthId) .where(sql`${table.status} = 'active'`), ], ); export type BerthReservation = typeof berthReservations.$inferSelect; export type NewBerthReservation = typeof berthReservations.$inferInsert;