import { Body, Controller, Delete, Get, Param, Patch, Post, Query, Req, UseGuards, UsePipes, ValidationPipe } from '@nestjs/common';
import { ApiBearerAuth, ApiBody, ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
import { AuthGuard } from 'src/auth/auth.guard';
import { RolesGuard } from 'src/guard/role.guard';
import { Roles } from 'src/guard/decorators/role.decorator';
import { CommonService } from 'src/common/common.service';
import { UserType } from 'src/user/schema/users.schema';
import { StaffService } from './staff.service';
import { CreateStaffDto, StaffListDto, UpdateStaffDto } from './dto/staff.dto';

@ApiTags('Staff')
@ApiBearerAuth('access_token')
@UseGuards(AuthGuard, RolesGuard)
@Roles(UserType.SUPER_ADMIN, UserType.ADMIN_STAFF)
@UsePipes(new ValidationPipe({ transform: true, whitelist: true }))
@Controller('admin/staff')
export class StaffController {
    constructor(
        private readonly staffService: StaffService,
        private readonly common: CommonService
    ) { }

    @Post()
    @ApiOperation({ summary: 'Add an in-house staff member (Policy Staff or Sub-Admin) and email an invite link. Only the Master Admin can add Sub-Admins.' })
    @ApiBody({ type: CreateStaffDto })
    @ApiResponse({ status: 201, description: 'Staff added and invite sent' })
    @ApiResponse({ status: 400, description: 'Email already registered' })
    @ApiResponse({ status: 403, description: 'Only Master Admin can manage Sub-Admins' })
    create(@Req() req: any, @Body() dto: CreateStaffDto) {
        return this.staffService.create(dto, req.user_data, this.common.getUserLanguage(req));
    }

    @Get()
    @ApiOperation({ summary: 'List staff (filter by user_type, status and search)' })
    list(@Req() req: any, @Query() query: StaffListDto) {
        return this.staffService.list(query, this.common.getUserLanguage(req));
    }

    @Get(':id')
    @ApiOperation({ summary: 'Staff profile with activity log' })
    @ApiResponse({ status: 404, description: 'Staff not found' })
    getById(@Req() req: any, @Param('id') id: string) {
        return this.staffService.getById(id, this.common.getUserLanguage(req));
    }

    @Patch(':id')
    @ApiOperation({ summary: 'Edit staff, change role, or activate/deactivate' })
    @ApiBody({ type: UpdateStaffDto })
    @ApiResponse({ status: 403, description: 'Only Master Admin can manage Sub-Admins' })
    update(@Req() req: any, @Param('id') id: string, @Body() dto: UpdateStaffDto) {
        return this.staffService.update(id, dto, req.user_data, this.common.getUserLanguage(req));
    }

    @Post(':id/resend-invite')
    @ApiOperation({ summary: 'Resend the invite link to a staff member who has not accepted yet' })
    resendInvite(@Req() req: any, @Param('id') id: string) {
        return this.staffService.resendInvite(id, req.user_data, this.common.getUserLanguage(req));
    }

    @Delete(':id')
    @ApiOperation({ summary: 'Remove a staff member. Only the Master Admin can remove Sub-Admins.' })
    remove(@Req() req: any, @Param('id') id: string) {
        return this.staffService.remove(id, req.user_data, this.common.getUserLanguage(req));
    }
}
