import {
  pgTable,
  text,
  timestamp,
  index,
  uniqueIndex,
  type AnyPgColumn,
} 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 berthTenancies = pgTable(
  'berth_tenancies',
  {
    id: text('id')
      .primaryKey()
      .$defaultFn(() => crypto.randomUUID()),
    // H-01: tenancies are the canonical "who occupies a berth right
    // now" record; RESTRICT on every parent FK keeps an ad-hoc DB-side
    // hard-delete from leaving a tenancy pointing at a missing
    // berth/client/yacht. Interest is nullable + SET NULL because a
    // tenancy legitimately outlives the originating deal.
    berthId: text('berth_id')
      .notNull()
      .references(() => berths.id, { onDelete: 'restrict' }),
    portId: text('port_id')
      .notNull()
      .references(() => ports.id, { onDelete: 'restrict' }),
    clientId: text('client_id')
      .notNull()
      .references(() => clients.id, { onDelete: 'restrict' }),
    yachtId: text('yacht_id')
      .notNull()
      .references(() => yachts.id, { onDelete: 'restrict' }),
    interestId: text('interest_id').references(() => interests.id, { onDelete: 'set null' }),
    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' }),
    // M-L01: canonical tenure_type union is
    // `permanent | fixed_term | fee_simple | strata_lot | seasonal`
    // (kept in sync with berths.tenure_type). 'seasonal' is tenancy-
    // specific (winter haul-out etc.); the others mirror the berth's
    // own tenure shape. Configurable via the per-port vocabulary at
    // /admin/vocabularies (key: berth_tenure_types).
    tenureType: text('tenure_type').notNull().default('permanent'),
    contractFileId: text('contract_file_id').references(() => files.id, { onDelete: 'set null' }),
    notes: text('notes'),
    // Renewal + transfer self-FKs (migration 0086). `previousTenancyId`
    // chains a successor row to the row it succeeds (fixed-term + seasonal
    // renewals mint a new row; permanent renewals mutate in place so the
    // FK stays null). `transferredFromTenancyId` chains a new row to the
    // predecessor when an active tenancy is handed over to a different
    // client / yacht. SET NULL on delete keeps the lineage best-effort.
    previousTenancyId: text('previous_tenancy_id').references(
      (): AnyPgColumn => berthTenancies.id,
      {
        onDelete: 'set null',
      },
    ),
    transferredFromTenancyId: text('transferred_from_tenancy_id').references(
      (): AnyPgColumn => berthTenancies.id,
      { onDelete: 'set null' },
    ),
    createdBy: text('created_by').notNull(),
    createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
    updatedAt: timestamp('updated_at', { withTimezone: true }).notNull().defaultNow(),
  },
  (table) => [
    index('idx_bt_berth').on(table.berthId),
    index('idx_bt_client').on(table.clientId),
    index('idx_bt_yacht').on(table.yachtId),
    index('idx_bt_port').on(table.portId),
    index('idx_bt_interest').on(table.interestId),
    index('idx_bt_contract_file').on(table.contractFileId),
    uniqueIndex('idx_bt_active')
      .on(table.berthId)
      .where(sql`${table.status} = 'active'`),
  ],
);

export type BerthTenancy = typeof berthTenancies.$inferSelect;
export type NewBerthTenancy = typeof berthTenancies.$inferInsert;
