import { Resolver, Query, Args, ID, Info, Mutation } from '@nestjs/graphql';
import { BuildTypes } from '@/models/build-types.model';
import graphqlFields from 'graphql-fields';
import { Types } from 'mongoose';
import { BuildTypesService } from './build-types.service';
import { CreateBuildTypesInput } from './dto/create-build-types.input';
import { UpdateBuildTypesInput } from './dto/update-build-types.input';
import type { GraphQLResolveInfo } from 'graphql';
import { ListBuildTypesResponse } from './dto/list-users.response';
import { ListArguments } from '@/dto/list.input';

@Resolver()
export class BuildTypesResolver {

    constructor(private readonly buildTypesService: BuildTypesService) { }

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

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

    @Mutation(() => BuildTypes, { name: 'createBuildType' })
    async createBuildType(@Args('input') input: CreateBuildTypesInput): Promise<BuildTypes> { return this.buildTypesService.create(input) }

    @Mutation(() => BuildTypes, { name: 'updateBuildType' })
    async updateBuildType(@Args('uuid') uuid: string, @Args('input') input: UpdateBuildTypesInput): Promise<BuildTypes> { return this.buildTypesService.update(uuid, input) }

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

}
