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

export type FollowRequestDocument = HydratedDocument<FollowRequest>;

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

  @Prop({ type: Types.ObjectId, ref: User.name, required: true, index: true })
  targetUserId!: Types.ObjectId;

  @Prop({ type: String, enum: ['pending', 'approved', 'rejected'], default: 'pending', index: true })
  status!: 'pending' | 'approved' | 'rejected';
}

export const FollowRequestSchema = SchemaFactory.createForClass(FollowRequest);
FollowRequestSchema.index({ requesterId: 1, targetUserId: 1 }, { unique: true });
FollowRequestSchema.index({ targetUserId: 1, status: 1, createdAt: -1 });
