import {
  ACAttrFilter,
  ACGuard,
  ApiPaginatedResponse,
  PageDto,
  PageMetaDto,
  PageOptionsDto,
} from '@gadmin2n/nest-common';
import {
  Body,
  Controller,
  Delete,
  Inject,
  NotFoundException,
  Param,
  ParseIntPipe,
  Patch,
  Post,
  UseGuards,
} from '@nestjs/common';
import {
  ApiCreatedResponse,
  ApiNotFoundResponse,
  ApiOkResponse,
  ApiTags,
} from '@nestjs/swagger';
import { Prisma } from '@prisma/client';
import { UseRoles } from 'nest-access-control';
import { WINSTON_MODULE_PROVIDER } from 'nest-winston';
import { Logger } from 'winston';
import {
  EffectedCount,
  UploudResponse,
  WorkflowEventOutboxFindManyArgs,
  WorkflowEventOutboxWhereSelect,
} from '../../generated/workflowEventOutbox/dto/workflowEventOutbox-types.dto';
import { WorkflowEventOutboxEntity } from '../../generated/workflowEventOutbox/entities/workflowEventOutbox.entity';
import { WorkflowEventOutboxService } from './workflowEventOutbox.service';

import { ConfigService } from '@nestjs/config';

@UseGuards(ACGuard)
@Controller('workflowEventOutbox')
@ApiTags('workflowEventOutbox')
export class WorkflowEventOutboxController {
  constructor(
    private readonly workflowEventOutboxService: WorkflowEventOutboxService,
    private readonly config: ConfigService,
    @Inject(WINSTON_MODULE_PROVIDER) private readonly logger: Logger,
  ) {}

  @Post('createOne')
  @UseRoles({
    resource: 'workflowEventOutbox',
    action: 'create',
    possession: 'any',
  })
  @ApiCreatedResponse({ type: WorkflowEventOutboxEntity })
  async createOne() {
    // 该资源为只读，创建接口已禁用
    throw new NotFoundException();
  }

  @Post('findMany')
  @UseRoles({
    resource: 'workflowEventOutbox',
    action: 'read',
    possession: 'any',
  })
  @ApiPaginatedResponse(WorkflowEventOutboxEntity)
  async findMany(
    @Body() body: WorkflowEventOutboxFindManyArgs,
    @ACAttrFilter() acAttrFilter,
  ): Promise<PageDto<WorkflowEventOutboxEntity>> {
    const { itemCount, entities } =
      await this.workflowEventOutboxService.findMany(
        body as Prisma.WorkflowEventOutboxFindManyArgs,
      );
    const { skip = 0, take = 10 } =
      body as Prisma.WorkflowEventOutboxFindManyArgs;
    const pageOptionsDto: PageOptionsDto = { skip, take };
    const pageMetaDto = new PageMetaDto({ itemCount, pageOptionsDto });

    return new PageDto(
      entities.map((item) => new WorkflowEventOutboxEntity(acAttrFilter(item))),
      pageMetaDto,
    );
  }

  @Post('/findUnique/:id')
  @UseRoles({
    resource: 'workflowEventOutbox',
    action: 'read',
    possession: 'any',
  })
  @ApiOkResponse({ type: WorkflowEventOutboxEntity })
  @ApiNotFoundResponse({ description: 'Cannot found this record.' })
  async findUnique(
    @Param('id', ParseIntPipe) id: number,
    @Body() body: WorkflowEventOutboxWhereSelect,
    @ACAttrFilter() acAttrFilter,
  ) {
    const item = await this.workflowEventOutboxService.findUnique(id, body);
    if (!item) {
      throw new NotFoundException();
    }
    return new WorkflowEventOutboxEntity(acAttrFilter(item));
  }

  @Patch('updateUnique/:id')
  @UseRoles({
    resource: 'workflowEventOutbox',
    action: 'update',
    possession: 'any',
  })
  @ApiOkResponse({ type: WorkflowEventOutboxEntity })
  @ApiNotFoundResponse({ description: 'Cannot found this record.' })
  async updateUnique() {
    // 该资源为只读，更新接口已禁用
    throw new NotFoundException();
  }

  @Delete('deleteUnique/:id')
  @UseRoles({
    resource: 'workflowEventOutbox',
    action: 'delete',
    possession: 'any',
  })
  @ApiOkResponse({ type: EffectedCount })
  @ApiNotFoundResponse({ description: 'Cannot found this record.' })
  async deleteUnique() {
    // 该资源为只读，删除接口已禁用
    throw new NotFoundException();
  }

  @Post('upload')
  @UseRoles({
    resource: 'workflowEventOutbox',
    action: 'create',
    possession: 'any',
  })
  @ApiCreatedResponse({ type: UploudResponse })
  async uploadFile() {
    // 该资源为只读，上传接口已禁用
    throw new NotFoundException();
  }
}
