import { Resolver, Query, Args, ID, Info, Mutation } from '@nestjs/graphql';
import { Types } from 'mongoose';
import { BuildSites } from '@/models/build-site.model';
import { BuildSitesService } from './build-sites.service';
import { ListArguments } from '@/dto/list.input';
import { ListBuildSitesResponse } from './dto/list-build-sites.response';
import { CreateBuildSitesInput } from './dto/create-build-sites.input';
import { UpdateBuildSitesInput } from './dto/update-build-sites.input';
import graphqlFields from 'graphql-fields';
import type { GraphQLResolveInfo } from 'graphql';

@Resolver()
export class BuildSitesResolver {

    constructor(private readonly buildSitesService: BuildSitesService) { }

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

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

    @Mutation(() => BuildSites, { name: 'createBuildSite' })
    async createBuildSite(@Args('input') input: CreateBuildSitesInput): Promise<BuildSites> { return this.buildSitesService.create(input) }

    @Mutation(() => BuildSites, { name: 'updateBuildSite' })
    async updateBuildSite(@Args('uuid') uuid: string, @Args('input') input: UpdateBuildSitesInput): Promise<BuildSites> { return this.buildSitesService.update(uuid, input) }

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

}
