import { Entity, Column, Index, ManyToOne, JoinColumn } from 'typeorm';
import { BaseEntity } from '../database/base.entity';
import { UserEntity } from './user.entity';

export enum NotificationKind {
  /** New change request submitted — goes to managers. */
  CHANGE_REQUEST_PENDING = 'change_request_pending',
  /** Manager decided on a change request — goes to the requester. */
  CHANGE_REQUEST_DECIDED = 'change_request_decided',
}

/**
 * NotificationEntity — in-app only for Phase 1.
 *
 * Each row is a message addressed to a specific user. We poll this
 * table via TanStack Query (staleTime 30s) to drive the bell icon;
 * email/SMS delivery is a separate concern (see TODO in
 * ChangeRequestService — SMTP isn't configured yet).
 *
 * Append-only from the user's side: they can mark a row read but
 * can't edit it. A later reaper could prune old read rows.
 */
@Entity('notifications')
@Index(['tenant_id', 'user_id', 'is_read'])
export class NotificationEntity extends BaseEntity {
  @ManyToOne(() => UserEntity, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'user_id' })
  user: UserEntity;

  @Column({ type: 'uuid' })
  user_id: string;

  @Column({ type: 'enum', enum: NotificationKind })
  kind: NotificationKind;

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

  @Column({ type: 'text' })
  message: string;

  /** Deep-link for the notification — frontend navigates here on click. */
  @Column({ type: 'varchar', length: 500, nullable: true })
  link: string | null;

  /** Related entity for future filtering — optional. */
  @Column({ type: 'varchar', length: 100, nullable: true })
  related_entity_type: string | null;

  @Column({ type: 'uuid', nullable: true })
  related_entity_id: string | null;

  @Column({ type: 'boolean', default: false })
  is_read: boolean;

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