import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { HydratedDocument } from 'mongoose';
import { EmployeePresence, UserRole, UserStatus } from '../../common/constants/enums';

export type UserDocument = HydratedDocument<User>;

@Schema({ timestamps: true, collection: 'users' })
export class User {
  @Prop({ required: true, unique: true, index: true })
  uid: string;

  @Prop({ required: true, unique: true, lowercase: true, trim: true })
  email: string;

  @Prop({ required: true, select: false })
  passwordHash: string;

  @Prop({ required: true, trim: true })
  fullName: string;

  /** Legacy Flutter/Mongo field — kept for older documents that only store `name`. */
  @Prop({ trim: true })
  name?: string;

  @Prop({ required: true, unique: true, trim: true, index: true })
  employeeCode: string;

  @Prop({ unique: true, sparse: true, trim: true, index: true })
  mobileNumber?: string;

  @Prop({ trim: true })
  sittingLocation?: string;

  @Prop({ index: true })
  reportingManagerId?: string;

  @Prop({ type: String, enum: UserRole, default: UserRole.EMPLOYEE, index: true })
  role: UserRole;

  @Prop({ trim: true })
  deviceId?: string;

  @Prop({ trim: true })
  profileImage?: string;

  @Prop({ type: String, enum: UserStatus, default: UserStatus.ACTIVE, index: true })
  status: UserStatus;

  @Prop({ trim: true })
  fcmToken?: string;

  @Prop({ default: false })
  isRooted: boolean;

  @Prop({
    type: String,
    enum: EmployeePresence,
    default: EmployeePresence.OFFLINE,
    index: true,
  })
  presence: EmployeePresence;

  @Prop()
  lastSeenAt?: Date;
}

export const UserSchema = SchemaFactory.createForClass(User);
