import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { ObjectType, Field, ID } from '@nestjs/graphql';
import { Document, Types } from 'mongoose';
import { Base } from '@/models/base.model';
import { Person } from '@/models/person.model';

@ObjectType()
export class CollectionToken {

    @Field()
    defaultSelection: string;

    @Field(() => [String])
    selectedBuildIDS: string[];

    @Field(() => [String])
    selectedCompanyIDS: string[];

}


@ObjectType()
@Schema({ timestamps: true })
export class User extends Base {

    @Field(() => ID)
    _id: string;

    @Field({ nullable: true })
    @Prop({ required: false, default: '' })
    avatar?: string;

    @Field({ nullable: true })
    @Prop({ default: () => new Date(Date.now() + 30 * 24 * 60 * 60 * 1000) })
    expiresAt?: Date;

    @Field({ nullable: true })
    @Prop()
    resetToken?: string;

    @Field()
    @Prop({ required: true })
    password: string;

    @Field(() => [String], { nullable: 'itemsAndList' })
    @Prop({ type: [String], default: [], validate: [(val: string[]) => val.length <= 3, 'History can have max 3 items'] })
    history?: string[];

    @Field()
    @Prop({ required: true })
    tag: string;

    @Field()
    @Prop({ required: true, unique: true })
    email: string;

    @Field()
    @Prop({ required: true, unique: true })
    phone: string;

    @Field(() => CollectionToken)
    @Prop({ type: CollectionToken, default: () => ({ defaultSelection: '', selectedBuildIDS: [], selectedCompanyIDS: [] }) })
    collectionTokens: CollectionToken;

    @Field(() => ID)
    @Prop({ type: Types.ObjectId, ref: Person.name, required: true })
    person: Types.ObjectId;

}

export type UserDocument = User & Document;
export const UserSchema = SchemaFactory.createForClass(User);
