import { Controller, Post, Param, Body, Patch, Delete, Req, UnauthorizedException, Query } from "@nestjs/common";
import { ExpenseDto } from "src/time-and-attendance/expenses/expense-create.dto";
import { Result } from "src/common/errors/result-error.factory";
import { ModuleAccessAllowed } from "src/common/decorators/modules-guard.decorators";
import { Modules as ModulesEnum } from "prisma/postgres/generated/prisma/client/postgres/client";
import { Access } from "src/common/decorators/module-access.decorators";
import { ExpenseUpdateService } from "src/time-and-attendance/expenses/services/expense-update.service";
import { ExpenseCreateService } from "src/time-and-attendance/expenses/services/expense-create.service";
import { ExpenseDeleteService } from "src/time-and-attendance/expenses/services/expense-delete.service";

@Controller('expense')
export class ExpenseController {
    constructor(
        private readonly updateService: ExpenseUpdateService,
        private readonly createService: ExpenseCreateService,
        private readonly deleteService: ExpenseDeleteService,
    ) { }

    @Post('create')
    @ModuleAccessAllowed(ModulesEnum.timesheets)
    create(@Access('email') email: string, @Body() dto: ExpenseDto): Promise<Result<ExpenseDto, string>> {
        if (!email) throw new UnauthorizedException('Unauthorized User');
        return this.createService.createExpense(dto, email);
    }

    @Patch('update')
    @ModuleAccessAllowed(ModulesEnum.timesheets)
    update(
        @Body() dto: ExpenseDto,
        @Access('email') email: string,
        @Query('employee_email') employee_email?: string,
    ): Promise<Result<ExpenseDto, string>> {
        return this.updateService.updateExpense(dto, email, employee_email);
    }

    @Delete('delete/:expense_id')
    @ModuleAccessAllowed(ModulesEnum.timesheets)
    remove(
        @Param('expense_id') expense_id: number,
        @Access('email') email: string,
    ): Promise<Result<number, string>> {
        return this.deleteService.deleteExpense(expense_id, email);
    }
}


