import {
    Controller,
    Get,
    Post,
    Delete,
    Param,
    Query,
    Body,
    Req,
    UseGuards,
    HttpCode,
    HttpStatus,
    ForbiddenException,
} from '@nestjs/common';
import { AdminService } from './admin.service';
import { JwtGuard } from 'src/common/guard/jwt/jwt.guard';
import { ResponseUtils } from 'src/common/utils/utils.response';
import { AuthContext } from 'src/modules/auth/types/auth.types';
import type { Request } from 'express';

@Controller('admin')
export class AdminController {
    constructor(private readonly adminService: AdminService) { }

    private checkAdmin(user: AuthContext) {
        if (!user.roles?.some((r) => ['ADMIN', 'SUPERADMIN'].includes(r))) {
            throw new ForbiddenException('Only admins can access this resource');
        }
    }

    private checkSuperadmin(user: AuthContext) {
        if (!user.roles?.includes('SUPERADMIN')) {
            throw new ForbiddenException('Only SUPERADMIN can access this resource');
        }
    }

    @Get('payments')
    @UseGuards(JwtGuard)
    @HttpCode(HttpStatus.OK)
    async getPayments(
        @Req() req: Request,
        @Query('status') status?: string,
    ) {
        this.checkAdmin(req.user as AuthContext);
        const payments = await this.adminService.getPayments(status);
        return ResponseUtils.successfully(payments, 'Payments retrieved');
    }

    @Post('payments/:id/confirm')
    @UseGuards(JwtGuard)
    @HttpCode(HttpStatus.OK)
    async confirmPayment(@Req() req: Request, @Param('id') paymentId: string) {
        const { username } = req.user as AuthContext;
        this.checkAdmin(req.user as AuthContext);
        const result = await this.adminService.confirmPayment(paymentId, username);
        return ResponseUtils.successfully(result, 'Payment confirmed successfully');
    }

    @Post('payments/:id/reject')
    @UseGuards(JwtGuard)
    @HttpCode(HttpStatus.OK)
    async rejectPayment(@Req() req: Request, @Param('id') paymentId: string) {
        const { username } = req.user as AuthContext;
        this.checkAdmin(req.user as AuthContext);
        const result = await this.adminService.rejectPayment(paymentId, username);
        return ResponseUtils.successfully(result, 'Payment rejected successfully');
    }

    @Get('roles')
    @UseGuards(JwtGuard)
    @HttpCode(HttpStatus.OK)
    async getRoles(@Req() req: Request) {
        this.checkAdmin(req.user as AuthContext);
        const roles = await this.adminService.getRoles();
        return ResponseUtils.successfully(roles, 'Roles retrieved');
    }

    @Get('menus')
    @UseGuards(JwtGuard)
    @HttpCode(HttpStatus.OK)
    async getMenus(@Req() req: Request) {
        this.checkAdmin(req.user as AuthContext);
        const menus = await this.adminService.getMenus();
        return ResponseUtils.successfully(menus, 'Menus retrieved');
    }

    @Get('role-menus/:roleId')
    @UseGuards(JwtGuard)
    @HttpCode(HttpStatus.OK)
    async getRoleMenus(@Req() req: Request, @Param('roleId') roleId: string) {
        this.checkAdmin(req.user as AuthContext);
        const mappings = await this.adminService.getRoleMenus(roleId);
        return ResponseUtils.successfully(mappings, 'Role-menu mappings retrieved');
    }

    @Post('role-menus')
    @UseGuards(JwtGuard)
    @HttpCode(HttpStatus.CREATED)
    async assignMenuToRole(
        @Req() req: Request,
        @Body() body: { roleId: string; menuId: string },
    ) {
        this.checkSuperadmin(req.user as AuthContext);
        const result = await this.adminService.assignMenuToRole(body.roleId, body.menuId);
        return ResponseUtils.created(result, 'Menu assigned to role successfully');
    }

    @Delete('role-menus/:roleId/:menuId')
    @UseGuards(JwtGuard)
    @HttpCode(HttpStatus.OK)
    async removeMenuFromRole(
        @Req() req: Request,
        @Param('roleId') roleId: string,
        @Param('menuId') menuId: string,
    ) {
        this.checkSuperadmin(req.user as AuthContext);
        await this.adminService.removeMenuFromRole(roleId, menuId);
        return ResponseUtils.successfully(null, 'Menu removed from role successfully');
    }
}
