import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  DeleteDateColumn,
  Index,
  OneToMany,
  Unique,
} from 'typeorm';
import { BiometricCredential } from './biometric-credential.entity';
import { DeviceSession } from './device-session.entity';

export type DevicePlatform = 'ios' | 'android' | 'web' | 'desktop';
export type BiometricType = 'fingerprint' | 'face_id' | 'face_recognition' | 'iris';

@Entity({ name: 'devices', schema: 'auth' })
@Unique(['userId', 'deviceUuid'])
export class Device {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

  @Index()
  @Column({ name: 'tenant_id', type: 'uuid', nullable: true })
  tenantId: string;

  // Identificacion del dispositivo
  @Index()
  @Column({ name: 'device_uuid', type: 'varchar', length: 100 })
  deviceUuid: string;

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

  @Column({ name: 'device_model', type: 'varchar', length: 100, nullable: true })
  deviceModel: string;

  @Column({ name: 'device_brand', type: 'varchar', length: 50, nullable: true })
  deviceBrand: string;

  // Plataforma
  @Index()
  @Column({ type: 'varchar', length: 20 })
  platform: DevicePlatform;

  @Column({ name: 'platform_version', type: 'varchar', length: 20, nullable: true })
  platformVersion: string;

  @Column({ name: 'app_version', type: 'varchar', length: 20, nullable: true })
  appVersion: string;

  // Estado
  @Index()
  @Column({ name: 'is_active', type: 'boolean', default: true })
  isActive: boolean;

  @Column({ name: 'is_trusted', type: 'boolean', default: false })
  isTrusted: boolean;

  @Column({ name: 'trust_level', type: 'integer', default: 0 })
  trustLevel: number; // 0=none, 1=low, 2=medium, 3=high

  // Biometricos habilitados
  @Column({ name: 'biometric_enabled', type: 'boolean', default: false })
  biometricEnabled: boolean;

  @Column({ name: 'biometric_type', type: 'varchar', length: 50, nullable: true })
  biometricType: BiometricType;

  // Push notifications
  @Column({ name: 'push_token', type: 'text', nullable: true })
  pushToken: string;

  @Column({ name: 'push_token_updated_at', type: 'timestamptz', nullable: true })
  pushTokenUpdatedAt: Date;

  // Ubicacion ultima conocida
  @Column({ name: 'last_latitude', type: 'decimal', precision: 10, scale: 8, nullable: true })
  lastLatitude: number;

  @Column({ name: 'last_longitude', type: 'decimal', precision: 11, scale: 8, nullable: true })
  lastLongitude: number;

  @Column({ name: 'last_location_at', type: 'timestamptz', nullable: true })
  lastLocationAt: Date;

  // Seguridad
  @Column({ name: 'last_ip_address', type: 'inet', nullable: true })
  lastIpAddress: string;

  @Column({ name: 'last_user_agent', type: 'text', nullable: true })
  lastUserAgent: string;

  // Registro
  @Column({ name: 'first_seen_at', type: 'timestamptz', default: () => 'CURRENT_TIMESTAMP' })
  firstSeenAt: Date;

  @Column({ name: 'last_seen_at', type: 'timestamptz', default: () => 'CURRENT_TIMESTAMP' })
  lastSeenAt: Date;

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

  @UpdateDateColumn({ name: 'updated_at', type: 'timestamptz' })
  updatedAt: Date;

  @DeleteDateColumn({ name: 'deleted_at', type: 'timestamptz', nullable: true })
  deletedAt: Date;

  // Relaciones
  @OneToMany(() => BiometricCredential, (credential) => credential.device)
  biometricCredentials: BiometricCredential[];

  @OneToMany(() => DeviceSession, (session) => session.device)
  sessions: DeviceSession[];
}
