import { Body, Controller, Delete, Param, Patch, Post } from "@nestjs/common";
import { Modules as ModulesEnum } from "prisma/postgres/generated/prisma/client/postgres/client";

import { ShiftDto } from "src/time-and-attendance/shifts/shift.dto";
import { ShiftsCreateService } from "src/time-and-attendance/shifts/services/shifts-create.service";
import { ShiftsUpdateService } from "src/time-and-attendance/shifts/services/shifts-update.service";
import { ShiftsDeleteService } from "src/time-and-attendance/shifts/services/shifts-delete.service";

import { ModuleAccessAllowed } from "src/common/decorators/modules-guard.decorators";
import { Result } from "src/common/errors/result-error.factory";
import { Access } from "src/common/decorators/module-access.decorators";

@Controller('shift')
export class ShiftController {
    constructor(
        private readonly create_service: ShiftsCreateService,
        private readonly update_service: ShiftsUpdateService,
        private readonly delete_service: ShiftsDeleteService,
    ) { }

    @Post('create')
    @ModuleAccessAllowed(ModulesEnum.timesheets)
    createBatch(@Access('email') email: string, @Body() dtos: ShiftDto[]): Promise<Result<boolean, string>> {
        return this.create_service.createOneOrManyShifts(email, dtos);
    }

    @Post('create/:email')
    @ModuleAccessAllowed(ModulesEnum.timesheets_approval)
    createBatchByTimesheetsApproval(@Param('email') email: string, @Body() dtos: ShiftDto[]): Promise<Result<boolean, string>> {
        return this.create_service.createOneOrManyShifts(email, dtos, false);
    }

    @Patch('update')
    @ModuleAccessAllowed(ModulesEnum.timesheets)
    updateBatch(@Access('email') email: string, @Body() dtos: ShiftDto[]): Promise<Result<boolean, string>> {
        return this.update_service.updateOneOrManyShifts(dtos, email);
    }

    @Patch('update/:email')
    @ModuleAccessAllowed(ModulesEnum.timesheets_approval)
    updateBatchByTimesheetApproval(@Param('email') email: string, @Body() dtos: ShiftDto[]): Promise<Result<boolean, string>> {
        return this.update_service.updateOneOrManyShifts(dtos, email, false);
    }

    @Delete(':shift_id')
    @ModuleAccessAllowed(ModulesEnum.timesheets)
    remove(@Access('email') email: string, @Param('shift_id') shift_id: number): Promise<Result<number, string>> {
        return this.delete_service.deleteShift(shift_id, email);
    }

    @Delete(':shift_id/:email')
    @ModuleAccessAllowed(ModulesEnum.timesheets)
    removeByTimesheetApproval(@Param('shift_id') shift_id: number, @Param('email') email: string): Promise<Result<number, string>> {
        return this.delete_service.deleteShift(shift_id, email, false);
    }
}
