import { ApiValidationError } from "../../Models/Server.Error.model";
import { Body, Controller, Middlewares, Post, Response, Route, SuccessResponse } from "tsoa";
import { SchedulesService } from "./Schedules.Service";
import {
    ISchedule,
    CreateScheduleParams,
    UpdateScheduleParams,
    DeleteScheduleParams,
    PaginateSchedulesParams,
    PaginateSchedulesResults,
    FindSchedulesParams,
    CollaboratorSchedulesView,
} from "../../Models/Schedules/Schedules.Interface";
import { authenticateMiddleware } from "../../middleware/authentication";
import { ApiVoidResult } from "../../Models/Api.VoidResult.type";

@Route("schedules/create")
@Middlewares(authenticateMiddleware)
export class CreateScheduleController extends Controller {
    @Response<ApiValidationError>(500, "Ha ocurrido un error")
    @SuccessResponse(200, "El empleado fue creado con exito")
    @Post()
    public async createSchedule(
        @Body() requestBody: CreateScheduleParams
    ): Promise<ISchedule | ApiValidationError> {
        try {
            const schedule = await new SchedulesService().createSchedule(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/get-all-by-collaborator")
export class GetAllSchedulsByCollaboratoreController extends Controller {
    @Response<ApiValidationError>(500, "Ha ocurrido un error")
    @SuccessResponse(200, "El empleado fue creado con exito")
    @Post()
    public async getAllSchedulesByCollaborator(
        @Body() requestBody: FindSchedulesParams
    ): Promise<CollaboratorSchedulesView | ApiValidationError> {
        try {
            const schedule = await new SchedulesService().findAllSchedulesByCollaborator(
                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/update")
@Middlewares(authenticateMiddleware)
export class UpdateScheduleController extends Controller {
    @Response<ApiValidationError>(500, "Ha ocurrido un error")
    @SuccessResponse(200, "La compañia fue modificada con exito")
    @Post()
    public async updateSchedule(
        @Body() requestBody: UpdateScheduleParams
    ): Promise<ApiVoidResult | ApiValidationError> {
        try {
            await new SchedulesService().updateSchedule(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/delete")
@Middlewares(authenticateMiddleware)
export class DeleteScheduleController extends Controller {
    @Response<ApiValidationError>(500, "Ha ocurrido un error")
    @SuccessResponse(200, "Done")
    @Post()
    public async deleteSchedule(
        @Body() requestBody: DeleteScheduleParams
    ): Promise<ApiVoidResult | ApiValidationError> {
        try {
            await new SchedulesService().deleteSchedule(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/paginate")
@Middlewares(authenticateMiddleware)
export class PaginateScheduleController extends Controller {
    @Response<ApiValidationError>(500, "Ha ocurrido un error")
    @SuccessResponse(200, "Done")
    @Post()
    public async paginateSchedules(
        @Body() requestBody: PaginateSchedulesParams
    ): Promise<PaginateSchedulesResults | ApiValidationError> {
        try {
            const schedules = await new SchedulesService().paginateSchedules(requestBody);
            this.setStatus(200);
            return schedules;
        } catch (e) {
            const errorOccurred: Error = e as Error;
            this.setStatus(500);
            return new ApiValidationError(500, errorOccurred.message);
        }
    }
}
