import { Resolver, Query, Args, ID, Info, Mutation } from '@nestjs/graphql';
import { Types } from 'mongoose';
import { Build } from '@/models/build.model';
import { CreateBuildInput } from './dto/create-build.input';
import graphqlFields from 'graphql-fields';
import { BuildService } from './build.service';
import type { GraphQLResolveInfo } from 'graphql';

@Resolver()
export class BuildResolver {

    constructor(private readonly buildService: BuildService) { }

    @Query(() => [Build], { name: 'Builds' })
    async getBuilds(@Info() info: GraphQLResolveInfo): Promise<Build[]> {
        const fields = graphqlFields(info); const projection = this.buildService.buildProjection(fields); return this.buildService.findAll(projection);
    }

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

    @Mutation(() => Build, { name: 'createBuild' })
    async createBuild(@Args('input') input: CreateBuildInput): Promise<Build> {
        return this.buildService.create(input);
    }

}
