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 { CreateUserTypesInput } from './dto/create-user-types.input';
import { ListUserTypeResponse } from './dto/list-build-sites.response';
import { UpdateUserTypeInput } from './dto/update-build-sites.input';
import { ListArguments } from '@/dto/list.input';
import type { GraphQLResolveInfo } from 'graphql';
import graphqlFields from 'graphql-fields';

@Resolver()
export class UserTypesResolver {

    constructor(private readonly userTypesService: UserTypesService) { }

    @Query(() => ListUserTypeResponse, { name: 'getUserTypes' })
    async getUserTypes(@Info() info: GraphQLResolveInfo, @Args('input') input: ListArguments): Promise<ListUserTypeResponse> {
        const fields = graphqlFields(info); const projection = this.userTypesService.buildProjection(fields?.data); const { skip, limit, sort, filters } = input;
        return await this.userTypesService.findAll(projection, skip, limit, sort, filters);
    }

    @Query(() => UserType, { name: 'getUserType', 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) }

    @Mutation(() => UserType, { name: 'updateUserType' })
    async updateUserType(@Args('uuid') uuid: string, @Args('input') input: UpdateUserTypeInput): Promise<UserType> { return this.userTypesService.update(uuid, input) }

    @Mutation(() => Boolean, { name: 'deleteUserType' })
    async deleteUserType(@Args('uuid') uuid: string): Promise<boolean> { return this.userTypesService.delete(uuid) }

}
