import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  ManyToOne,
  JoinColumn,
  CreateDateColumn,
} from 'typeorm';
import { User } from '../../../database/entities/user.entity';

@Entity('webauthn_credentials')
export class WebAuthnCredential {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

  @Column({ name: 'user_id' })
  userId: string;

  @Column({ name: 'credential_id', unique: true })
  credentialId: string;

  @Column({ name: 'public_key' })
  publicKey: string;

  @Column({ name: 'counter', type: 'bigint', default: 0 })
  counter: number;

  @Column({ name: 'device_type', nullable: true })
  deviceType?: string;

  @Column({ name: 'transports', type: 'text', array: true, nullable: true })
  transports?: string[];

  @Column({ name: 'backed_up', default: false })
  backedUp: boolean;

  @Column({ name: 'authenticator_attachment', nullable: true })
  authenticatorAttachment?: string;

  @CreateDateColumn({ name: 'created_at' })
  createdAt: Date;

  @Column({
    name: 'last_used',
    type: 'timestamp with time zone',
    nullable: true,
  })
  lastUsed?: Date;

  @Column({ name: 'friendly_name', nullable: true })
  friendlyName?: string;
}
