import {
  CreateDateColumn,
  UpdateDateColumn,
  Entity,
  Index,
  Column,
  RelationId,
  ManyToOne,
  OneToMany,
  PrimaryGeneratedColumn
} from 'typeorm'
import { ObjectType, Field, Int, ID, registerEnumType } from 'type-graphql'

import { Domain } from '@things-factory/shell'
import { User } from '@things-factory/auth-base'
import { Process } from '../process/process'

@Entity()
@Index('ix_process_group_0', (group: ProcessGroup) => [group.domain, group.name], { unique: true })
@ObjectType({ description: 'Entity for Process Management ProcessGroup' })
export class ProcessGroup {
  @PrimaryGeneratedColumn('uuid')
  @Field(type => ID)
  readonly id: string

  @ManyToOne(type => Domain)
  @Field({ nullable: true })
  domain?: Domain

  @RelationId((group: ProcessGroup) => group.domain)
  domainId?: string

  @Column()
  @Field()
  name: string

  @Column({
    nullable: true
  })
  @Field({ nullable: true })
  description?: string

  @OneToMany(type => Process, process => process.group)
  @Field(type => [Process], { nullable: true })
  processes: Process[]

  @CreateDateColumn()
  @Field({ nullable: true })
  createdAt?: Date

  @UpdateDateColumn()
  @Field({ nullable: true })
  updatedAt?: Date

  @ManyToOne(type => User, {
    nullable: true
  })
  @Field({ nullable: true })
  creator?: User

  @RelationId((group: ProcessGroup) => group.creator)
  creatorId?: string

  @ManyToOne(type => User, {
    nullable: true
  })
  @Field({ nullable: true })
  updater?: User

  @RelationId((group: ProcessGroup) => group.updater)
  updaterId?: string
}
