import { Body, Controller, Get, Patch, Query, Req } from "@nestjs/common";
import { Access } from "src/common/decorators/module-access.decorators";
import { Result } from "src/common/errors/result-error.factory";
import { ModuleAccess } from "src/identity-and-account/user-module-access/module-acces.dto";
import { AccessGetService } from "src/identity-and-account/user-module-access/services/module-access-get.service";
import { AccessUpdateService } from "src/identity-and-account/user-module-access/services/module-access-update.service";
import { ModuleAccessAllowed } from "src/common/decorators/modules-guard.decorators";
import { Modules as ModulesEnum } from "prisma/postgres/generated/prisma/client/postgres/client";

@Controller('module_access')
export class ModuleAccessController {
    constructor(
        private readonly getService: AccessGetService,
        private readonly updateService: AccessUpdateService,
    ) { }

    @Get()
    @ModuleAccessAllowed(ModulesEnum.employee_management)
    async findAccess(@Access('email') email: string, @Query('employee_email') employee_email?: string
    ): Promise<Result<boolean, string>> {
        await this.getService.findModuleAccess(email, employee_email);
        return { success: true, data: true };
    };

    @Patch('update')
    @ModuleAccessAllowed(ModulesEnum.employee_management)
    async updateAccess(@Access('email') email: string, @Body() dto: ModuleAccess, @Query('employee_email') employee_email?: string
    ): Promise<Result<boolean, string>> {
        await this.updateService.updateModuleAccess(email, dto, employee_email);
        return { success: true, data: true };
    };
}