model Complex {
  id              String        @id @db.Uuid
  complexName     String        @map("complex_name")
  physicalAddress String?       @map("physical_address") @db.VarChar(200)
  city            String?       @map("city") @db.VarChar(100)
  state           String?       @map("state") @db.VarChar(100)
  country         String?       @map("country") @db.VarChar(100)
  complexSlug     String        @unique @map("complex_slug")
  adminEmail      String        @map("admin_email")
  planCode        String?       @map("plan_code") @db.VarChar(10)
  createdAt       DateTime      @default(now()) @map("created_at")
  updatedAt       DateTime      @updatedAt @map("updated_at")
  plan            Plan?         @relation(fields: [planCode], references: [code])
  users           ComplexUser[]
  invitations     ComplexInvitation[]
  courts          Court[]

  @@index([planCode])
  @@index([complexSlug])
  @@map("complexes")
}

enum ComplexUserRole {
  ADMIN
  EMPLOYEE
}

model ComplexUser {
  complexId String          @map("complex_id") @db.Uuid
  userId    String          @map("user_id")
  role      ComplexUserRole @default(ADMIN)
  createdAt DateTime        @default(now()) @map("created_at")
  complex   Complex         @relation(fields: [complexId], references: [id], onDelete: Cascade)
  user      User            @relation(fields: [userId], references: [id], onDelete: Cascade)

  @@id([complexId, userId])
  @@index([userId])
  @@map("complex_users")
}

model ComplexInvitation {
  id         String   @id @db.Uuid
  complexId   String   @map("complex_id") @db.Uuid
  email       String   @db.VarChar(255)
  tokenHash   String   @unique @map("token_hash")
  expiresAt   DateTime @map("expires_at")
  acceptedAt  DateTime? @map("accepted_at")
  revokedAt   DateTime? @map("revoked_at")
  createdAt   DateTime @default(now()) @map("created_at")
  updatedAt   DateTime @updatedAt @map("updated_at")
  complex     Complex  @relation(fields: [complexId], references: [id], onDelete: Cascade)

  @@index([complexId, email])
  @@index([email])
  @@map("complex_invitations")
}
