import { Body, Controller, Get, Put, Query } from '@nestjs/common';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { ExcludePrivilege, Public } from 'src/core/guards';
import { MODULE_NAME } from 'src/core/strings/constants/module.constants';
import { UserPrivilegeConfigurationDataOrchestrator } from '../domain/usecases/user-privilege-configuration/user-privilege-configuration-data.orchestrator';
import { UserPrivilegeConfigurationDto } from './dto/user-privilege-configuration.dto';
import { UserPrivilegeConfigurationEntity } from '../domain/entities/user-privilege-configuration.entity';
import { FilterUserPrivilegeConfigurationDto } from './dto/filter-user-privilege-configuration.dto';
import { PaginationResponse } from 'src/core/response/domain/ok-response.interface';
import { Pagination } from 'src/core/response';
import { FilterMenuUserPrivilegeConfigurationDto } from './dto/filter-menu-user-privilege-configuration.dto';

@ApiTags(
  `${MODULE_NAME.USER_PRIVILEGE_CONFIGURATION.split('-').join(' ')} - data`,
)
@Controller(`v1/${MODULE_NAME.USER_PRIVILEGE_CONFIGURATION}`)
@Public(false)
@ApiBearerAuth('JWT')
export class UserPrivilegeConfigurationController {
  constructor(
    private orchestrator: UserPrivilegeConfigurationDataOrchestrator,
  ) {}

  @Put()
  async update(
    @Body() data: UserPrivilegeConfigurationDto,
  ): Promise<UserPrivilegeConfigurationEntity> {
    return await this.orchestrator.update(data);
  }

  @Put('/bulk')
  async updateBulk(
    @Body() datum: UserPrivilegeConfigurationDto[],
  ): Promise<UserPrivilegeConfigurationEntity[]> {
    const results = [];
    for (const data of datum) {
      const result = await this.orchestrator.update(data);
      results.push(result);
    }

    return results;
  }

  @Get()
  @Pagination()
  async index(
    @Query() params: FilterUserPrivilegeConfigurationDto,
  ): Promise<PaginationResponse<UserPrivilegeConfigurationEntity>> {
    return await this.orchestrator.index(params);
  }

  @Get('/menu')
  @ExcludePrivilege()
  async privilegeMenu(
    @Query() params: FilterMenuUserPrivilegeConfigurationDto,
  ): Promise<UserPrivilegeConfigurationEntity[]> {
    return (await this.orchestrator.privilegeMenu(params)).data ?? [];
  }
}
