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

export type DropdownOptionDocument = DropdownOption & Document;

@Schema({ timestamps: true })
export class DropdownOption extends BaseEntity {
    @Prop({ required: true, trim: true, index: true })
    category: string;

    @Prop({ required: true, trim: true })
    value: string;

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

export const DropdownOptionSchema = SchemaFactory.createForClass(DropdownOption);

// Unique per (category, value) — case-insensitive
DropdownOptionSchema.index(
    { category: 1, value: 1 },
    { unique: true, collation: { locale: 'en', strength: 2 } },
);

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

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