import {
  ACAttrFilter,
  ACGuard,
  ApiPaginatedResponse,
  PageDto,
  PageMetaDto,
  PageOptionsDto,
} from '@gadmin2n/nest-common';
import {
  Body,
  Controller,
  Delete,
  Inject,
  NotFoundException,
  Param,
  ParseUUIDPipe,
  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 {
  AgendaJobFindManyArgs,
  AgendaJobWhereSelect,
  EffectedCount,
  UploudResponse,
} from '../../generated/agendaJob/dto/agendaJob-types.dto';
import { AgendaJobEntity } from '../../generated/agendaJob/entities/agendaJob.entity';
import { AgendaJobService } from './agendaJob.service';

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

@UseGuards(ACGuard)
@Controller('agendaJob')
@ApiTags('agendaJob')
export class AgendaJobController {
  constructor(
    private readonly agendaJobService: AgendaJobService,
    private readonly config: ConfigService,
    @Inject(WINSTON_MODULE_PROVIDER) private readonly logger: Logger,
  ) {}

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

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

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

  @Post('/findUnique/:id')
  @UseRoles({
    resource: 'agendaJob',
    action: 'read',
    possession: 'any',
  })
  @ApiOkResponse({ type: AgendaJobEntity })
  @ApiNotFoundResponse({ description: 'Cannot found this record.' })
  async findUnique(
    @Param('id', ParseUUIDPipe) id: string,
    @Body() body: AgendaJobWhereSelect,
    @ACAttrFilter() acAttrFilter,
  ) {
    const item = await this.agendaJobService.findUnique(id, body);
    if (!item) {
      throw new NotFoundException();
    }
    return new AgendaJobEntity(acAttrFilter(item));
  }

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

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

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