import { Resolver, Query, Args, ID, Info, Mutation } from '@nestjs/graphql';
import { Types } from 'mongoose';
import { UserType } from '@/models/user-type.model';
import { UserTypesService } from '@/user-types/user-types.service';
import type { GraphQLResolveInfo } from 'graphql';
import graphqlFields from 'graphql-fields';
import { CreateUserTypesInput } from './dto/create-user-types.input';

@Resolver()
export class UserTypesResolver {

    constructor(private readonly userTypesService: UserTypesService) { }

    @Query(() => [UserType], { name: 'userTypes' })
    async getUsers(@Info() info: GraphQLResolveInfo): Promise<UserType[]> {
        const fields = graphqlFields(info); const projection = this.userTypesService.buildProjection(fields); return this.userTypesService.findAll(projection);
    }

    @Query(() => UserType, { name: 'userType', nullable: true })
    async getUserType(@Args('id', { type: () => ID }) id: string, @Info() info: GraphQLResolveInfo): Promise<UserType | null> {
        const fields = graphqlFields(info); const projection = this.userTypesService.buildProjection(fields); return this.userTypesService.findById(new Types.ObjectId(id), projection);
    }

    @Mutation(() => UserType, { name: 'createUserType' })
    async createUserType(@Args('input') input: CreateUserTypesInput): Promise<UserType> {
        return this.userTypesService.create(input);
    }

}
