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

import { Domain, ScalarObject, ScalarDate } from '@things-factory/shell'
import { User } from '@things-factory/auth-base'

export enum NotificationStatus {
  NOTREAD = 'UNREAD',
  READ = 'READ'
}

registerEnumType(NotificationStatus, {
  name: 'NotificationStatus',
  description: 'state enumeration of a notification'
})

@Entity()
@Index('ix_notification_0', (notification: Notification) => [notification.domain, notification.type], { unique: false })
@ObjectType({ description: 'Entity for Notification' })
export class Notification {
  @PrimaryGeneratedColumn('uuid')
  @Field(type => ID)
  readonly id: string

  @ManyToOne(type => Domain)
  @Field(type => Domain)
  domain?: Domain

  @RelationId((notification: Notification) => notification.domain)
  domainId?: string

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

  @RelationId((notification: Notification) => notification.owner)
  ownerId?: string

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

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

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

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

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

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

  @Column({ nullable: true })
  @Field({ nullable: true })
  state?: NotificationStatus

  @Column('simple-json', { nullable: true })
  @Field(type => ScalarObject, { nullable: true })
  property?: any

  @Column('date', { nullable: true })
  @Field(type => ScalarDate, { nullable: true })
  timestamp?: Date

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

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

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

  @RelationId((notification: Notification) => notification.creator)
  creatorId?: string

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

  @RelationId((notification: Notification) => notification.updater)
  updaterId?: string
}
