import { Resolver, Query, Args, ID, Info, Mutation } from '@nestjs/graphql';
import { Types } from 'mongoose';
import { BuildParts } from '@/models/build-parts.model';
import { CreateBuildPartsInput } from './dto/create-build-part.input';
import { BuildPartsService } from './build-parts.service';
import { ListArguments } from '@/dto/list.input';
import { ListBuildPartsResponse } from './dto/list-build-parts.response';
import graphqlFields from 'graphql-fields';
import type { GraphQLResolveInfo } from 'graphql';
import { UpdateBuildPartsInput } from './dto/update-build-parts.input';

@Resolver()
export class BuildPartsResolver {

    constructor(private readonly buildPartsService: BuildPartsService) { }

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

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

    @Mutation(() => BuildParts, { name: 'createBuildPart' })
    async createBuildPart(@Args('input') input: CreateBuildPartsInput): Promise<BuildParts> { return this.buildPartsService.create(input) }

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

    @Mutation(() => BuildParts, { name: 'updateBuildPart' })
    async updateBuildPart(@Args('uuid') uuid: string, @Args('input') input: UpdateBuildPartsInput): Promise<BuildParts> { return this.buildPartsService.update(uuid, input) }

}
