import { Resolver, Query, Args, ID, Info, Mutation } from '@nestjs/graphql';
import { Types } from 'mongoose';
import { BuildAddress } from '@/models/build-address.model';
import { ListArguments } from '@/dto/list.input';
import { UpdateBuildAddressInput } from './dto/update-build-address.input';
import { ListBuildAddressInput } from './dto/list-build-address.response';
import { CreateBuildAddressInput } from './dto/create-build-address.input';
import graphqlFields from 'graphql-fields';
import { BuildAddressService } from './build-address.service';
import type { GraphQLResolveInfo } from 'graphql';

@Resolver()
export class BuildAddressResolver {

    constructor(private readonly buildAddressService: BuildAddressService) { }

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

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

    @Mutation(() => BuildAddress, { name: 'createBuildAddress' })
    async createBuildAddress(@Args('input') input: CreateBuildAddressInput): Promise<BuildAddress> { return this.buildAddressService.create(input) }

    @Mutation(() => BuildAddress, { name: 'updateBuildAddress' })
    async updateBuildAddress(@Args('uuid') uuid: string, @Args('input') input: UpdateBuildAddressInput): Promise<BuildAddress> { return this.buildAddressService.update(uuid, input) }

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

}
