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

export enum TrustLevel {
  STANDARD = 'standard',
  HIGH = 'high',
  TEMPORARY = 'temporary',
}

@Entity({ schema: 'auth', name: 'trusted_devices' })
@Index('idx_trusted_devices_user', ['userId'], { where: 'is_active' })
@Index('idx_trusted_devices_fingerprint', ['deviceFingerprint'])
@Index('idx_trusted_devices_expires', ['trustExpiresAt'], {
  where: 'trust_expires_at IS NOT NULL AND is_active',
})
export class TrustedDevice {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column({ type: 'uuid', nullable: false, name: 'user_id' })
  userId: string;

  @Column({
    type: 'varchar',
    length: 128,
    nullable: false,
    name: 'device_fingerprint',
  })
  deviceFingerprint: string;

  @Column({ type: 'varchar', length: 128, nullable: true, name: 'device_name' })
  deviceName: string | null;

  @Column({ type: 'varchar', length: 32, nullable: true, name: 'device_type' })
  deviceType: string | null;

  @Column({ type: 'text', nullable: true, name: 'user_agent' })
  userAgent: string | null;

  @Column({ type: 'varchar', length: 64, nullable: true, name: 'browser_name' })
  browserName: string | null;

  @Column({
    type: 'varchar',
    length: 32,
    nullable: true,
    name: 'browser_version',
  })
  browserVersion: string | null;

  @Column({ type: 'varchar', length: 64, nullable: true, name: 'os_name' })
  osName: string | null;

  @Column({ type: 'varchar', length: 32, nullable: true, name: 'os_version' })
  osVersion: string | null;

  @Column({ type: 'inet', nullable: false, name: 'registered_ip' })
  registeredIp: string;

  @Column({ type: 'jsonb', nullable: true, name: 'registered_location' })
  registeredLocation: Record<string, unknown> | null;

  @Column({ type: 'boolean', default: true, nullable: false, name: 'is_active' })
  isActive: boolean;

  @Column({
    type: 'enum',
    enum: TrustLevel,
    default: TrustLevel.STANDARD,
    nullable: false,
    name: 'trust_level',
  })
  trustLevel: TrustLevel;

  @Column({ type: 'timestamptz', nullable: true, name: 'trust_expires_at' })
  trustExpiresAt: Date | null;

  @Column({ type: 'timestamptz', nullable: false, name: 'last_used_at' })
  lastUsedAt: Date;

  @Column({ type: 'inet', nullable: true, name: 'last_used_ip' })
  lastUsedIp: string | null;

  @Column({ type: 'integer', default: 1, nullable: false, name: 'use_count' })
  useCount: number;

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

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

  @Column({ type: 'timestamptz', nullable: true, name: 'revoked_at' })
  revokedAt: Date | null;

  @Column({ type: 'varchar', length: 128, nullable: true, name: 'revoked_reason' })
  revokedReason: string | null;
}
