import { Resolver, Args, ID, Info, Mutation, Query } from '@nestjs/graphql';
import { Types } from 'mongoose';
import { ListArguments } from '@/dto/list.input';
import { LivingSpaces } from '@/models/living-spaces.model';
import { ListLivingSpaceResponse } from './dto/list-living-space.response';
import { CreateLivingSpaceInput } from './dto/create-living-space.input';
import { UpdateLivingSpaceInput } from './dto/update-living-space.input';
import { LivingSpaceService } from './living-space.service';
import type { GraphQLResolveInfo } from 'graphql';
import graphqlFields from 'graphql-fields';

@Resolver()
export class LivingSpaceResolver {

    constructor(private readonly livingSpaceService: LivingSpaceService) { }

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

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

    @Mutation(() => LivingSpaces, { name: 'createLivingSpace' })
    async createLivingSpace(@Args('input') input: CreateLivingSpaceInput): Promise<LivingSpaces> { return this.livingSpaceService.create(input) }

    @Mutation(() => LivingSpaces, { name: 'updateLivingSpace' })
    async updateLivingSpace(@Args('buildID') buildID: string, @Args('uuid') uuid: string, @Args('input') input: UpdateLivingSpaceInput): Promise<LivingSpaces> { return this.livingSpaceService.update(buildID, uuid, input) }

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

}
