import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document, Schema as MongooseSchema, Types } from 'mongoose';

// Audit trail for a user (staff or agent) shown on their detail screen.
// `action` maps to the i18n key ACTIVITY_<action>; `meta` fills its placeholders.
@Schema()
export class ActivityLog extends Document {
    @Prop({ type: Types.ObjectId, ref: 'users', required: true })
    subject_id: Types.ObjectId; // the user the entry is about

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

    @Prop({ type: Types.ObjectId, default: null })
    actor_id: Types.ObjectId; // who did it (an admin, or the subject themselves)

    @Prop({ type: String, default: null })
    actor_name: string;

    @Prop({ type: MongooseSchema.Types.Mixed, default: null })
    meta: Record<string, any>;

    @Prop({ type: Number, default: () => +new Date() })
    created_at: number;
}

export const ActivityLogSchema = SchemaFactory.createForClass(ActivityLog);
ActivityLogSchema.index({ subject_id: 1, created_at: -1 });
