import { ApiValidationError } from "../../Models/Server.Error.model";
import { Body, Controller, Middlewares, Post, Response, Route, SuccessResponse } from "tsoa";
import { SchedulesOverridesService } from "./SchedulesOverrides.Service";
import {
    ISchedulesOverrides,
    CreateSchedulesOverridesParams,
    UpdateSchedulesOverridesParams,
    DeleteSchedulesOverridesParams,
    FindSchedulesOverridesParams,
} from "../../Models/SchedulesOverrides/SchedulesOverrides.Interface";
import { authenticateMiddleware } from "../../middleware/authentication";
import { ApiVoidResult } from "../../Models/Api.VoidResult.type";

@Route("schedules-overrides/create")
@Middlewares(authenticateMiddleware)
export class CreateSchedulesOverridesController extends Controller {
    @Response<ApiValidationError>(500, "Ha ocurrido un error")
    @SuccessResponse(200, "La excepción de horario fue creada con exito")
    @Post()
    public async createSchedulesOverrides(
        @Body() requestBody: CreateSchedulesOverridesParams
    ): Promise<ISchedulesOverrides | ApiValidationError> {
        try {
            const schedule = await new SchedulesOverridesService().createSchedulesOverrides(requestBody);
            this.setStatus(200);
            return schedule;
        } catch (e) {
            const errorOccurred: Error = e as Error;
            this.setStatus(500);
            return new ApiValidationError(500, errorOccurred.message);
        }
    }
}

@Route("schedules-overrides/update")
@Middlewares(authenticateMiddleware)
export class UpdateSchedulesOverridesController extends Controller {
    @Response<ApiValidationError>(500, "Ha ocurrido un error")
    @SuccessResponse(200, "La excepción de horario fue modificada con exito")
    @Post()
    public async updateSchedulesOverrides(
        @Body() requestBody: UpdateSchedulesOverridesParams
    ): Promise<ApiVoidResult | ApiValidationError> {
        try {
            await new SchedulesOverridesService().updateSchedulesOverrides(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-overrides/delete")
@Middlewares(authenticateMiddleware)
export class DeleteSchedulesOverridesController extends Controller {
    @Response<ApiValidationError>(500, "Ha ocurrido un error")
    @SuccessResponse(200, "Done")
    @Post()
    public async deleteSchedulesOverrides(
        @Body() requestBody: DeleteSchedulesOverridesParams
    ): Promise<ApiVoidResult | ApiValidationError> {
        try {
            await new SchedulesOverridesService().deleteSchedulesOverrides(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-overrides/get")
@Middlewares(authenticateMiddleware)
export class GetSchedulesOverridesController extends Controller {
    @Response<ApiValidationError>(500, "Ha ocurrido un error")
    @SuccessResponse(200, "Done")
    @Post()
    public async getSchedulesOverrides(
        @Body() requestBody: FindSchedulesOverridesParams
    ): Promise<ISchedulesOverrides[] | ApiValidationError> {
        try {
            const schedules = await new SchedulesOverridesService().getSchedulesOverrides(requestBody);
            this.setStatus(200);
            return schedules;
        } catch (e) {
            const errorOccurred: Error = e as Error;
            this.setStatus(500);
            return new ApiValidationError(500, errorOccurred.message);
        }
    }
}
