import { Resolver, Query, Args, ID, Info, Mutation } from '@nestjs/graphql';
import { BuildIban } from '@/models/build.model';
import graphqlFields from 'graphql-fields';
import { Types } from 'mongoose';
import { CreateBuildIbanInput } from './dto/create-build-ibans.input';
import { UpdateBuildIbanInput } from './dto/update-build-ibans.input';
import { ListBuildIbanResponse } from './dto/list-build-ibans.response';
import { ListArguments } from '@/dto/list.input';
import { BuildIbanService } from './build-iban.service';
import type { GraphQLResolveInfo } from 'graphql';

@Resolver()
export class BuildIbanResolver {

    constructor(private readonly buildIbanService: BuildIbanService) { }

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

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

    @Mutation(() => BuildIban, { name: 'createBuildIban' })
    async createBuildIban(@Args('input') input: CreateBuildIbanInput): Promise<BuildIban> { return this.buildIbanService.create(input) }

    @Mutation(() => BuildIban, { name: 'updateBuildIban' })
    async updateBuildIban(@Args('uuid') uuid: string, @Args('input') input: UpdateBuildIbanInput): Promise<BuildIban> { return this.buildIbanService.update(uuid, input) }

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

}
