import { Body, Controller, Get, Post } from '@nestjs/common';
import { CreateStructureDto } from './dto/create-structure.dto';
import { Structure } from './schemas/structure.schema';
import { StructuresService } from './structures.service';

@Controller('structures')
export class StructuresController {
  constructor(private readonly structureService: StructuresService) {}

  @Post()
  async create(@Body() createStructureDto: CreateStructureDto) {
    await this.structureService.create(createStructureDto);
  }

  @Get()
  async findAll(): Promise<Structure[]> {
    return this.structureService.findAll();
  }

  @Get('count')
  async countCategories(): Promise<Array<{ id: string; count: number }>> {
    const data = await Promise.all([
      this.structureService.countByStructureKey('accesAuxDroits'),
      this.structureService.countByStructureKey('aideALaParentalite'),
      this.structureService.countByStructureKey('cultureEtSecuriteNumerique'),
      this.structureService.countByStructureKey('insertionSocialeEtProfessionnelle'),
      this.structureService.countByStructureKey('accompagnementDesDemarches'),
      this.structureService.countByStructureKey('labelsEtQualifications'),
      this.structureService.countByStructureKey('publicsAcceptes'),
      this.structureService.countByStructureKey('modalitesDacces'),
      this.structureService.countByStructureKey('lesCompetencesDeBase'),
      this.structureService.countByEquipmentsKey('wifiEnAccesLibre'),
      this.structureService.countByEquipmentsKey('ordinateurs'),
      this.structureService.countByEquipmentsKey('tablettes'),
      this.structureService.countByEquipmentsKey('bornesNumeriques'),
      this.structureService.countByEquipmentsKey('imprimantes'),
    ]);
    // Return a concat of all arrays
    return data.reduce((a, b) => [...a, ...b]);
  }
}