import { Body, Controller, Get, Param, Patch, Post, Query, UseGuards } from '@nestjs/common';
import { ApiBearerAuth, ApiBody, ApiQuery, ApiTags } from '@nestjs/swagger';
import { JwtAuthGuard } from 'src/guards/jwt-auth.guard';
import { GeneralFeedbackService } from './service/general-feedback.service';
import { Roles } from 'src/common/decorators/roles.decorator';
import { RolesGuard } from 'src/guards/roles.guard';
import { PaginatedQueryDto } from 'src/common/builder/QueryBuilder.dto';
import { ResponseMessage } from 'src/common/decorators/response_message.decorator';
import { IFullUser } from '../users/users.interface';
import { CurrentUser } from 'src/common/decorators/user.decorator';
import { ChangeGeneralFeedbackStatusDto, CreateGeneralFeedbackDto, AddMessageDto } from './general-feedback.validation';
import { USER_ROLE } from '../users/users.constant';
import { IGeneralFeedbackStatus } from './general-feedback.interface';

@ApiTags('User General Feedback')
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Controller('general-feedback')
export class GeneralFeedbackController {

	constructor(
		private readonly generalFeedbackService: GeneralFeedbackService
	) { }

	@Get('all-feedbacks')
	@UseGuards(RolesGuard)
	@Roles(USER_ROLE.admin, USER_ROLE.super_admin)
	@ApiQuery({ type: PaginatedQueryDto })
	async getFeedbacks(
		@Query() query: Record<string, unknown>,
	) {
		return await this.generalFeedbackService.getFeedbacks(query)
	}

	@Get('feedback-by-id/:feedbackId')
	@UseGuards(RolesGuard)
	@Roles(USER_ROLE.admin, USER_ROLE.super_admin)
	async getFeedbackById(
	    @Param('feedbackId') feedbackId: string,
	) {
	    return await this.generalFeedbackService.getGeneralFeedbackById(feedbackId)
	}

	@Get('my-feedbacks')
	@UseGuards(RolesGuard)
	@Roles(USER_ROLE.user, USER_ROLE.admin, USER_ROLE.super_admin)
	@ApiQuery({ type: PaginatedQueryDto })
	async getMyFeedbacks(
		@CurrentUser() user: IFullUser,
		@Query() query: Record<string, unknown>,
	) {
		return await this.generalFeedbackService.getMyGeneralFeedbacks(user, query);
	}

	@Patch('change-status/:feedbackId')
	@UseGuards(RolesGuard)
	@Roles(USER_ROLE.super_admin)
	@ApiBody({ type: ChangeGeneralFeedbackStatusDto })
	async changeStatus(
	    @Param('feedbackId') feedbackId: string,
	    @Body() body: { status: IGeneralFeedbackStatus }
	) {
	    await this.generalFeedbackService.updateFeedbackStatus(feedbackId, body)
	    return { message: 'Status updated successfully' }
	}

	@Post('add-message/:feedbackId')
	@UseGuards(RolesGuard)
	@Roles(USER_ROLE.user, USER_ROLE.admin, USER_ROLE.super_admin)
	@ApiBody({ type: AddMessageDto })
	@ResponseMessage("Message added successfully")
	async addMessage(
		@Param('feedbackId') feedbackId: string,
		@Body() payload: AddMessageDto,
		@CurrentUser() user: IFullUser,
	) {
		return await this.generalFeedbackService.handleUserMessage(feedbackId, payload);
	}

	@Post('create-general-feedback')
	@UseGuards(RolesGuard)
	@Roles(USER_ROLE.user, USER_ROLE.admin, USER_ROLE.super_admin)
	@ApiBody({ type: CreateGeneralFeedbackDto })
	@ResponseMessage("General feedback created successfully")
	async createGeneralfeedback(
		@Body() payload: CreateGeneralFeedbackDto,
		@CurrentUser() user: IFullUser,
	) {
		return await this.generalFeedbackService.createGeneralFeedback(payload, user);
	}

	@Patch('mark-as-read/:feedbackId')
	@UseGuards(RolesGuard)
	@Roles(USER_ROLE.user, USER_ROLE.admin, USER_ROLE.super_admin)
	@ResponseMessage("Feedback marked as read")
	async markAsRead(
		@Param('feedbackId') feedbackId: string,
		@CurrentUser() user: IFullUser,
	) {
		return await this.generalFeedbackService.markAsRead(feedbackId, user);
	}
}
