import { Resolver, Args, ID, Info, Mutation, Query } from '@nestjs/graphql';
import { CompanyService } from './company.service';
import { Company } from '@/models/company.model';
import { CreateCompanyInput } from './dto/create-company.input';
import { Types } from 'mongoose';
import { ListCompanyResponse } from './dto/list-company.response';
import { ListArguments } from '@/dto/list.input';
import { UpdateCompanyInput } from './dto/update-company.input';
import type { GraphQLResolveInfo } from 'graphql';
import graphqlFields from 'graphql-fields';

@Resolver()
export class CompanyResolver {

    constructor(private readonly companyService: CompanyService) { }

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

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

    @Mutation(() => Company, { name: 'createCompany' })
    async createCompany(@Args('input') input: CreateCompanyInput): Promise<Company> { return this.companyService.create(input) }

    @Mutation(() => Company, { name: 'updateCompany' })
    async updateCompany(@Args('uuid') uuid: string, @Args('input') input: UpdateCompanyInput): Promise<Company> { return this.companyService.update(uuid, input) }

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

}
