import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document } from 'mongoose';
import { BaseEntity } from '../common/entities/base.entity';

export type DesignationDocument = Designation & Document;

@Schema({ timestamps: true })
export class Designation extends BaseEntity {
    @Prop({ required: true, trim: true, unique: true })
    name: string;

    @Prop({ default: false })
    isDefault: boolean;
}

export const DesignationSchema = SchemaFactory.createForClass(Designation);

// Case-insensitive unique name
DesignationSchema.index({ name: 1 }, { unique: true, collation: { locale: 'en', strength: 2 } });

// Virtual id for consistent client shape
DesignationSchema.virtual('id').get(function (this: any) {
    return this._id?.toString?.() ?? String(this._id);
});

DesignationSchema.set('toJSON', { versionKey: false, virtuals: true });
DesignationSchema.set('toObject', { versionKey: false, virtuals: true });
