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

@Entity({ schema: 'auth', name: 'devices' })
@Index('idx_devices_tenant_id', ['tenantId'])
@Index('idx_devices_user_id', ['userId'])
@Index('idx_devices_device_id', ['deviceId'])
export class Device {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

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

  @Column({ type: 'varchar', length: 255, nullable: false, name: 'device_id' })
  deviceId: string;

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

  @Column({ type: 'varchar', length: 50, nullable: false, name: 'device_type' })
  deviceType: string;

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

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

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

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

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

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

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

  @ManyToOne(() => Tenant, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'tenant_id' })
  tenant: Tenant;

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