import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  ManyToOne,
  JoinColumn,
  Unique,
  UpdateDateColumn,
} from 'typeorm';
import { HistoryContentType } from '../../common/enums';
import { User } from '../../users/entities/user.entity';

@Entity('history')
@Unique(['userId', 'contentId', 'contentType'])
export class History {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column()
  userId: string;

  @ManyToOne(() => User, (user) => user.history, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'userId' })
  user: User;

  @Column('uuid')
  contentId: string;

  @Column({ type: 'enum', enum: HistoryContentType })
  contentType: HistoryContentType;

  @Column({ default: 0 })
  progressSeconds: number;

  @Column({ default: 0 })
  totalSeconds: number;

  @UpdateDateColumn({ type: 'timestamptz' })
  watchedAt: Date;
}
