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 graphqlFields from 'graphql-fields';
import type { GraphQLResolveInfo } from 'graphql';
import { ListCompanyResponse } from './dto/list-company.response';
import { ListArguments } from '@/dto/list.input';

@Resolver()
export class CompanyResolver {

    constructor(private readonly companyService: CompanyService) { }

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

    @Query(() => Company, { name: 'company', 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) }

}
