/**
 * PushNotificationLog Entity
 * Push notification delivery log
 * Compatible with erp-core push-notification-log.entity
 *
 * @module Mobile
 */

import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  Index,
  ManyToOne,
  JoinColumn,
} from 'typeorm';
import { PushToken } from './push-token.entity';

export type PushNotificationStatus = 'sent' | 'delivered' | 'failed' | 'read';
export type PushNotificationCategory = 'attendance' | 'sale' | 'inventory' | 'alert' | 'system';

@Entity({ name: 'push_notifications_log', schema: 'mobile' })
export class PushNotificationLog {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

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

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

  @Column({ name: 'push_token_id', type: 'uuid', nullable: true })
  pushTokenId: string;

  @ManyToOne(() => PushToken)
  @JoinColumn({ name: 'push_token_id' })
  pushToken: PushToken;

  @Column({ type: 'varchar', length: 200 })
  title: string;

  @Column({ type: 'text', nullable: true })
  body: string;

  @Column({ type: 'jsonb', default: {} })
  data: Record<string, any>;

  @Index()
  @Column({ type: 'varchar', length: 50, nullable: true })
  category: PushNotificationCategory;

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

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

  @Column({ type: 'varchar', length: 20, default: 'sent' })
  status: PushNotificationStatus;

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

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

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

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