/**
 * NotificationPreference Entity
 * User notification preferences per tenant
 * Compatible with erp-core preference.entity
 *
 * @module Notifications
 */

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

export type DigestFrequency = 'instant' | 'hourly' | 'daily' | 'weekly';

@Entity({ name: 'notification_preferences', schema: 'notifications' })
@Unique(['userId', 'tenantId'])
export class NotificationPreference {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

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

  @Column({ name: 'global_enabled', type: 'boolean', default: true })
  globalEnabled: boolean;

  @Column({ name: 'quiet_hours_start', type: 'time', nullable: true })
  quietHoursStart: string;

  @Column({ name: 'quiet_hours_end', type: 'time', nullable: true })
  quietHoursEnd: string;

  @Column({ type: 'varchar', length: 50, default: 'America/Mexico_City' })
  timezone: string;

  @Column({ name: 'email_enabled', type: 'boolean', default: true })
  emailEnabled: boolean;

  @Column({ name: 'sms_enabled', type: 'boolean', default: false })
  smsEnabled: boolean;

  @Column({ name: 'push_enabled', type: 'boolean', default: true })
  pushEnabled: boolean;

  @Column({ name: 'whatsapp_enabled', type: 'boolean', default: false })
  whatsappEnabled: boolean;

  @Column({ name: 'in_app_enabled', type: 'boolean', default: true })
  inAppEnabled: boolean;

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

  @Column({ name: 'digest_frequency', type: 'varchar', length: 20, default: 'instant' })
  digestFrequency: DigestFrequency;

  @Column({ name: 'digest_day', type: 'int', nullable: true })
  digestDay: number;

  @Column({ name: 'digest_hour', type: 'int', nullable: true })
  digestHour: number;

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

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

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