import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { HydratedDocument, Types } from 'mongoose';
import { User } from '../../users/schemas/user.schema';

export type DeviceDocument = HydratedDocument<Device>;

@Schema({ timestamps: true, versionKey: false })
export class Device {
  @Prop({ type: Types.ObjectId, ref: User.name, required: true, index: true })
  userId!: Types.ObjectId;

  @Prop({ required: true, trim: true, index: true })
  fcmToken!: string;

  @Prop({ enum: ['android', 'ios', 'web'], required: true, index: true })
  platform!: 'android' | 'ios' | 'web';

  @Prop({ default: '', trim: true, index: true })
  deviceId!: string;

  @Prop({ default: '', trim: true })
  appVersion!: string;

  @Prop({ default: '', trim: true })
  locale!: string;

  @Prop({ default: true, index: true })
  isActive!: boolean;

  @Prop({ type: Date, default: null })
  lastSeenAt?: Date | null;
}

export const DeviceSchema = SchemaFactory.createForClass(Device);
DeviceSchema.index({ userId: 1, fcmToken: 1 }, { unique: true });
DeviceSchema.index({ userId: 1, deviceId: 1 }, { sparse: true });
