export const contracts = pgTable('contracts', {
id: uuid('id').primaryKey().defaultRandom(),
tenantId: uuid('tenant_id').notNull(),
vendorId: uuid('vendor_id'),
contractType: varchar('contract_type', { length: 50 }).notNull(), // nda|msa|sow|dpa|sla
title: varchar('title', { length: 500 }).notNull(),
status: varchar('status', { length: 30 }).notNull().default('draft'),
// draft|review|negotiation|pending_approval|pending_signature|active|amended|expired|terminated
effectiveDate: date('effective_date'),
expiryDate: date('expiry_date'),
autoRenewal: boolean('auto_renewal').default(false),
renewalNoticeDays: integer('renewal_notice_days').default(30),
totalValue: decimal('total_value', { precision: 18, scale: 2 }),
currency: varchar('currency', { length: 3 }).default('USD'),
templateId: uuid('template_id'),
currentVersion: integer('current_version').default(1),
signingRequestId: uuid('signing_request_id'), // links to esignature
ownerId: uuid('owner_id').notNull(),
metadata: jsonb('metadata'),
createdAt: timestamp('created_at').defaultNow(),
updatedAt: timestamp('updated_at').defaultNow(),
});
export const contractClauses = pgTable('contract_clauses', {
id: uuid('id').primaryKey().defaultRandom(),
tenantId: uuid('tenant_id').notNull(),
contractId: uuid('contract_id').references(() => contracts.id),
clauseLibraryId: uuid('clause_library_id'),
title: varchar('title', { length: 255 }).notNull(),
content: text('content').notNull(),
clauseType: varchar('clause_type', { length: 50 }).notNull(),
orderIndex: integer('order_index').notNull(),
isNegotiable: boolean('is_negotiable').default(true),
scfControlIds: jsonb('scf_control_ids'), // SCF control mapping
});
export const contractObligations = pgTable('contract_obligations', {
id: uuid('id').primaryKey().defaultRandom(),
tenantId: uuid('tenant_id').notNull(),
contractId: uuid('contract_id').references(() => contracts.id),
clauseId: uuid('clause_id').references(() => contractClauses.id),
description: text('description').notNull(),
obligationType: varchar('obligation_type', { length: 30 }).notNull(), // compliance|delivery|payment|reporting
ownerId: uuid('owner_id').notNull(),
dueDate: date('due_date'),
recurrence: varchar('recurrence', { length: 20 }), // once|monthly|quarterly|annually
status: varchar('status', { length: 20 }).notNull().default('pending'),
evidenceIds: jsonb('evidence_ids'),
createdAt: timestamp('created_at').defaultNow(),
});
export const contractVersions = pgTable('contract_versions', {
id: uuid('id').primaryKey().defaultRandom(),
tenantId: uuid('tenant_id').notNull(),
contractId: uuid('contract_id').references(() => contracts.id),
versionNumber: integer('version_number').notNull(),
content: text('content').notNull(), // full document content
changedBy: uuid('changed_by').notNull(),
changeNotes: text('change_notes'),
createdAt: timestamp('created_at').defaultNow(),
});