import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  Index,
  Unique,
} from 'typeorm';

export type PushProvider = 'firebase' | 'apns' | 'fcm';

@Entity({ name: 'push_tokens', schema: 'mobile' })
@Unique(['deviceId', 'platform'])
export class PushToken {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

  @Index()
  @Column({ name: 'device_id', type: 'uuid' })
  deviceId: string;

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

  // Token
  @Column({ type: 'text' })
  token: string;

  @Column({ type: 'varchar', length: 20 })
  platform: string; // ios, android

  @Column({ type: 'varchar', length: 30, default: 'firebase' })
  provider: PushProvider;

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

  @Column({ name: 'is_valid', type: 'boolean', default: true })
  isValid: boolean;

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

  // Topics suscritos
  @Column({ name: 'subscribed_topics', type: 'text', array: true, default: [] })
  subscribedTopics: string[];

  // Ultima actividad
  @Column({ name: 'last_used_at', type: 'timestamptz', nullable: true })
  lastUsedAt: Date;

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

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