import { AuthMethod, MultiTenancyInterceptor, UseAuth } from '@backend/auth'
import {
	Body,
	Controller,
	Get,
	HttpCode,
	Param,
	ParseArrayPipe,
	ParseUUIDPipe,
	Patch,
	Post,
	Query,
	UseInterceptors,
} from '@nestjs/common'
import {
	ApiBadRequestResponse,
	ApiInternalServerErrorResponse,
	ApiNoContentResponse,
	ApiNotFoundResponse,
	ApiOkResponse,
	ApiTags,
} from '@nestjs/swagger'
import { ParseDatePipe } from '../../../common'
import { MessageService } from '../services'
import { MessageResponse, SendMessageRequest } from '../types'

@UseAuth(AuthMethod.JWT)
@UseInterceptors(MultiTenancyInterceptor)
@Controller('message')
@ApiTags('default')
export class MessageController {
	constructor(private readonly _messageService: MessageService) {}

	@Get()
	@ApiOkResponse({ type: MessageResponse, isArray: true })
	@ApiInternalServerErrorResponse()
	async findAll(
		@Query('date', new ParseDatePipe()) date?: Date,
		@Query('tableIds', new ParseArrayPipe({ optional: true })) tableIds?: string[],
	): Promise<MessageResponse[]> {
		return this._messageService.findAll(tableIds, date)
	}

	@Post()
	@ApiOkResponse({ type: MessageResponse })
	@ApiBadRequestResponse()
	@ApiInternalServerErrorResponse()
	async send(@Body() requst: SendMessageRequest): Promise<MessageResponse> {
		return this._messageService.send(requst)
	}

	@Patch(':id/acknowledge')
	@HttpCode(204)
	@ApiNoContentResponse()
	@ApiNotFoundResponse()
	@ApiInternalServerErrorResponse()
	async acknowledge(@Param('id', ParseUUIDPipe) id: string): Promise<void> {
		return this._messageService.acknowledge(id)
	}
}
