import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document, Types } from 'mongoose';
import mongoose from 'mongoose';
import { NotificationType } from '../enums/notification-type.enum';

export type NotificationDocument = Notification & Document;

@Schema({ timestamps: { createdAt: 'created_at', updatedAt: 'updated_at' } })
export class Notification {
    @Prop({ type: String, enum: Object.values(NotificationType), required: true })
    type: NotificationType;

    @Prop({ type: String, required: true })
    title: string;

    @Prop({ type: String, required: true })
    message: string;

    @Prop({ type: mongoose.Schema.Types.Mixed, default: null })
    meta: Record<string, any>; // values for {placeholders} in the translated title and message

    @Prop({ type: Boolean, default: false })
    is_read: boolean;

    @Prop({ type: mongoose.Schema.Types.ObjectId, ref: 'users', required: true })
    sent_by: Types.ObjectId;

    @Prop({ type: mongoose.Schema.Types.ObjectId, ref: 'users', required: true })
    sent_to: Types.ObjectId;

    @Prop({ type: mongoose.Schema.Types.ObjectId, ref: 'orders' })
    order_id: Types.ObjectId;

    @Prop({ type: Number })
    created_at: number;

    @Prop({ type: Number })
    updated_at: number;
}

export const NotificationSchema = SchemaFactory.createForClass(Notification);

// Add index for faster queries
NotificationSchema.index({ sent_to: 1, created_at: -1 });
NotificationSchema.index({ sent_to: 1, is_read: 1 });
