import {
  CreateDateColumn,
  UpdateDateColumn,
  Entity,
  Index,
  Column,
  RelationId,
  ManyToOne,
  PrimaryGeneratedColumn
} from 'typeorm'

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

@Entity('route_label_sequence_counters')
@Index('ix_route_label_sequence_counters_0', (c: RouteLabelSequenceCounter) => [c.domain, c.releaseGoodId, c.field], {
  unique: true
})
@Index('ix_route_label_sequence_counters_1', (c: RouteLabelSequenceCounter) => [c.domain, c.field])
export class RouteLabelSequenceCounter {
  @PrimaryGeneratedColumn('uuid')
  readonly id: string

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

  @RelationId((c: RouteLabelSequenceCounter) => c.domain)
  domainId?: string

  @Column()
  releaseGoodId: string

  @Column()
  field: string

  @Column({ type: 'int', default: 0 })
  lastSeq: number

  @CreateDateColumn()
  createdAt?: Date

  @UpdateDateColumn()
  updatedAt?: Date

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

  @RelationId((c: RouteLabelSequenceCounter) => c.creator)
  creatorId?: string

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

  @RelationId((c: RouteLabelSequenceCounter) => c.updater)
  updaterId?: string
}
