import { ApiValidationError } from "../../Models/Server.Error.model";
import { Body, Controller, Middlewares, Post, Response, Route, SuccessResponse } from "tsoa";

import { authenticateMiddleware } from "../../middleware/authentication";
import { ApiVoidResult } from "../../Models/Api.VoidResult.type";
import {
    CreateSchedulesDisabledParams,
    DeleteSchedulesDisabledParams,
    DisableScheduleParams,
    FindSchedulesDisabledParams,
    ISchedulesDisabled,
    SchedulesDisabledByCollaboratorView,
} from "../../Models/SchedulesDisabled/SchedulesDisabled.Interface";
import { SchedulesDisabledService } from "./SchedulesDisabled.Service";

@Route("schedules-disabled/create")
@Middlewares(authenticateMiddleware)
export class CreateSchedulesDisabledController extends Controller {
    @Response<ApiValidationError>(500, "Ha ocurrido un error")
    @SuccessResponse(200, "El empleado fue creado con exito")
    @Post()
    public async createSchedulesDisabled(
        @Body() requestBody: CreateSchedulesDisabledParams
    ): Promise<ISchedulesDisabled | ApiValidationError> {
        try {
            const schedulesDisabled = await new SchedulesDisabledService().createSchedulesDisabled(
                requestBody
            );
            this.setStatus(200);
            return schedulesDisabled;
        } catch (e) {
            const errorOccurred: Error = e as Error;
            this.setStatus(500);
            return new ApiValidationError(500, errorOccurred.message);
        }
    }
}

@Route("schedules-disabled/delete")
@Middlewares(authenticateMiddleware)
export class DeleteSchedulesDisabledController extends Controller {
    @Response<ApiValidationError>(500, "Ha ocurrido un error")
    @SuccessResponse(200, "Done")
    @Post()
    public async deleteSchedulesDisabled(
        @Body() requestBody: DeleteSchedulesDisabledParams
    ): Promise<ApiVoidResult | ApiValidationError> {
        try {
            await new SchedulesDisabledService().deleteSchedulesDisabled(requestBody);
            this.setStatus(200);
            return {
                success: true,
                message: "Done",
            };
        } catch (e) {
            const errorOccurred: Error = e as Error;
            this.setStatus(500);
            return new ApiValidationError(500, errorOccurred.message);
        }
    }
}

@Route("schedules-disabled/get")
@Middlewares(authenticateMiddleware)
export class GetSchedulesDisabledController extends Controller {
    @Response<ApiValidationError>(500, "Ha ocurrido un error")
    @SuccessResponse(200, "Done")
    @Post()
    public async paginateSchedules(
        @Body() requestBody: FindSchedulesDisabledParams
    ): Promise<ISchedulesDisabled | null | ApiValidationError> {
        try {
            const schedulesDisabled = await new SchedulesDisabledService().getSchedulesDisabled(requestBody);
            this.setStatus(200);
            return schedulesDisabled;
        } catch (e) {
            const errorOccurred: Error = e as Error;
            this.setStatus(500);
            return new ApiValidationError(500, errorOccurred.message);
        }
    }
}

@Route("schedules-disabled/enable")
@Middlewares(authenticateMiddleware)
export class EnableSchedulesDisabledController extends Controller {
    @Response<ApiValidationError>(500, "Ha ocurrido un error")
    @SuccessResponse(200, "Done")
    @Post()
    public async enableSchedulesDisabled(
        @Body() requestBody: DisableScheduleParams
    ): Promise<ApiVoidResult | ApiValidationError> {
        try {
            await new SchedulesDisabledService().enableSchedule(requestBody);
            this.setStatus(200);
            return {
                success: true,
                message: "Done",
            };
        } catch (e) {
            const errorOccurred: Error = e as Error;
            this.setStatus(500);
            return new ApiValidationError(500, errorOccurred.message);
        }
    }
}

@Route("schedules-disabled/disable")
@Middlewares(authenticateMiddleware)
export class DisableSchedulesDisabledController extends Controller {
    @Response<ApiValidationError>(500, "Ha ocurrido un error")
    @SuccessResponse(200, "Done")
    @Post()
    public async disableSchedulesDisabled(
        @Body() requestBody: DisableScheduleParams
    ): Promise<ApiVoidResult | ApiValidationError> {
        try {
            await new SchedulesDisabledService().disableSchedule(requestBody);
            this.setStatus(200);
            return {
                success: true,
                message: "Done",
            };
        } catch (e) {
            const errorOccurred: Error = e as Error;
            this.setStatus(500);
            return new ApiValidationError(500, errorOccurred.message);
        }
    }
}

@Route("schedules-disabled/find-by-collaborator")
@Middlewares(authenticateMiddleware)
export class FindSchedulesDisabledByCollaboratorController extends Controller {
    @Response<ApiValidationError>(500, "Ha ocurrido un error")
    @SuccessResponse(200, "Done")
    @Post()
    public async findSchedulesDisabled(
        @Body() requestBody: FindSchedulesDisabledParams
    ): Promise<SchedulesDisabledByCollaboratorView | ApiValidationError> {
        try {
            const schedulesDisabled =
                await new SchedulesDisabledService().findSchedulesDisabledByCollaborator(requestBody);
            this.setStatus(200);
            return schedulesDisabled;
        } catch (e) {
            const errorOccurred: Error = e as Error;
            this.setStatus(500);
            return new ApiValidationError(500, errorOccurred.message);
        }
    }
}
