import { ApiValidationError } from "../../Models/Server.Error.model";
import { Body, Controller, Middlewares, Post, Response, Route, SuccessResponse } from "tsoa";
import { SchedulesEnabledService } from "./SchedulesEnabled.Service";
import {
    CreateSchedulesEnabledParams,
    DeleteSchedulesEnabledParams,
    EnableScheduleParams,
    FindSchedulesEnabledParams,
    ISchedulesEnabled,
    SchedulesEnabledView,
} from "../../Models/SchedulesEnabled/SchedulesEnabled.Interface";
import { authenticateMiddleware } from "../../middleware/authentication";
import { ApiVoidResult } from "../../Models/Api.VoidResult.type";

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

@Route("schedules-enabled/delete")
@Middlewares(authenticateMiddleware)
export class DeleteSchedulesEnabledController extends Controller {
    @Response<ApiValidationError>(500, "Ha ocurrido un error")
    @SuccessResponse(200, "Done")
    @Post()
    public async deleteSchedulesEnabled(
        @Body() requestBody: DeleteSchedulesEnabledParams
    ): Promise<ApiVoidResult | ApiValidationError> {
        try {
            await new SchedulesEnabledService().deleteSchedulesEnabled(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-enabled/get")
@Middlewares(authenticateMiddleware)
export class GetSchedulesEnabledController extends Controller {
    @Response<ApiValidationError>(500, "Ha ocurrido un error")
    @SuccessResponse(200, "Done")
    @Post()
    public async paginateSchedules(
        @Body() requestBody: FindSchedulesEnabledParams
    ): Promise<ISchedulesEnabled | null | ApiValidationError> {
        try {
            const schedulesEnabled = await new SchedulesEnabledService().getSchedulesEnabled(
                requestBody
            );
            this.setStatus(200);
            return schedulesEnabled;
        } catch (e) {
            const errorOccurred: Error = e as Error;
            this.setStatus(500);
            return new ApiValidationError(500, errorOccurred.message);
        }
    }
}

@Route("schedules-enabled/find")
@Middlewares(authenticateMiddleware)
export class FindSchedulesEnabledController extends Controller {
    @Response<ApiValidationError>(500, "Ha ocurrido un error")
    @SuccessResponse(200, "Done")
    @Post()
    public async findSchedulesEnabled(
        @Body() requestBody: FindSchedulesEnabledParams
    ): Promise<SchedulesEnabledView[] | ApiValidationError> {
        try {
            const schedulesEnabled = await new SchedulesEnabledService().find(requestBody);
            this.setStatus(200);
            return schedulesEnabled;
        } catch (e) {
            const errorOccurred: Error = e as Error;
            this.setStatus(500);
            return new ApiValidationError(500, errorOccurred.message);
        }
    }
}

@Route("schedules-enabled/enable")
@Middlewares(authenticateMiddleware)
export class EnableSchedulesEnabledController extends Controller {
    @Response<ApiValidationError>(500, "Ha ocurrido un error")
    @SuccessResponse(200, "Done")
    @Post()
    public async enableSchedulesEnabled(
        @Body() requestBody: EnableScheduleParams
    ): Promise<ApiVoidResult | ApiValidationError> {
        try {
            await new SchedulesEnabledService().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-enabled/disable")
@Middlewares(authenticateMiddleware)
export class DisableSchedulesEnabledController extends Controller {
    @Response<ApiValidationError>(500, "Ha ocurrido un error")
    @SuccessResponse(200, "Done")
    @Post()
    public async disableSchedulesEnabled(
        @Body() requestBody: EnableScheduleParams
    ): Promise<ApiVoidResult | ApiValidationError> {
        try {
            await new SchedulesEnabledService().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);
        }
    }
}
