import { Resolver, Query, Args, ID, Info, Mutation } from '@nestjs/graphql';
import { Types } from 'mongoose';
import { BuildArea } from '@/models/build-area.model';
import { BuildAreaService } from './build-area.service';
import { ListArguments } from '@/dto/list.input';
import { UpdateBuildAreaInput } from './dto/update-build-area.input';
import { ListBuildAreaResponse } from './dto/list-build-area.response';
import { CreateBuildAreaInput } from './dto/create-build-area.input';
import graphqlFields from 'graphql-fields';
import type { GraphQLResolveInfo } from 'graphql';

@Resolver()
export class BuildAreaResolver {

    constructor(private readonly buildAreaService: BuildAreaService) { }

    @Query(() => ListBuildAreaResponse, { name: 'buildAreas' })
    async getBuildAreas(@Info() info: GraphQLResolveInfo, @Args('input') input: ListArguments): Promise<ListBuildAreaResponse> {
        const fields = graphqlFields(info); const projection = this.buildAreaService.buildProjection(fields?.data); return await this.buildAreaService.findAll(projection, input);
    }

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

    @Mutation(() => BuildArea, { name: 'createBuildArea' })
    async createBuildArea(@Args('input') input: CreateBuildAreaInput): Promise<BuildArea> { return this.buildAreaService.create(input) }

    @Mutation(() => BuildArea, { name: 'updateBuildArea' })
    async updateBuildArea(@Args('uuid') uuid: string, @Args('input') input: UpdateBuildAreaInput): Promise<BuildArea> { return this.buildAreaService.update(uuid, input) }

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

}
