import { pgTable, uuid, text, timestamp, unique, boolean, integer } from 'drizzle-orm/pg-core';
import { organizations } from './organizations';

export const projects = pgTable(
  'projects',
  {
    id: uuid('id').primaryKey().defaultRandom(),

    // Project details
    name: text('name').notNull(),
    slug: text('slug').notNull(), // URL-friendly identifier for storage paths
    organizationId: uuid('organization_id')
      .notNull()
      .references(() => organizations.id, { onDelete: 'cascade' }),

    // Live scope settings (Section 8.4)
    allowNewLiveScopes: boolean('allow_new_live_scopes').notNull().default(true),
    newLiveScopesGenerationLimit: integer('new_live_scopes_generation_limit').notNull().default(30),

    // Timestamps
    createdAt: timestamp('created_at').notNull().defaultNow(),
    updatedAt: timestamp('updated_at')
      .notNull()
      .defaultNow()
      .$onUpdate(() => new Date()),
  },
  (table) => ({
    // Unique constraint: one project slug per organization
    uniqueOrgProjectSlug: unique().on(table.organizationId, table.slug),
  }),
);

export type Project = typeof projects.$inferSelect;
export type NewProject = typeof projects.$inferInsert;
