import { authenticateMiddleware } from "../../middleware/authentication";
import { ApiValidationError } from "../../Models/Server.Error.model";
import { Body, Controller, Middlewares, Post, Response, Route, SuccessResponse } from "tsoa";
import {
    AddRepeatDisabledDateParams,
    CreateRepeatsParams,
    DeleteRepeatsParams,
    FindRepeatsParams,
    IRepeat,
    RepeatView,
    GetPendingRepeatsParams,
    PendingRepeatView,
} from "../../Models/Repeats/Repeats.Interface";
import { RepeatsService } from "./Repeats.Service";
import { ApiVoidResult } from "../../Models/Api.VoidResult.type";

@Route("repeats/create")
@Middlewares(authenticateMiddleware)
export class CreateRepeatController extends Controller {
    @Response<ApiValidationError>(500, "Ha ocurrido un error")
    @SuccessResponse(200, "La repetición fue creada con éxito")
    @Post()
    public async createRepeat(
        @Body() requestBody: CreateRepeatsParams
    ): Promise<IRepeat | ApiValidationError> {
        try {
            const repeat = await new RepeatsService().createRepeat(requestBody);
            this.setStatus(200);
            return repeat;
        } catch (e) {
            const errorOccurred: Error = e as Error;
            this.setStatus(500);
            return new ApiValidationError(500, errorOccurred.message);
        }
    }
}

@Route("repeats/delete")
@Middlewares(authenticateMiddleware)
export class DeleteRepeatController extends Controller {
    @Response<ApiValidationError>(500, "Ha ocurrido un error")
    @SuccessResponse(200, "Done")
    @Post()
    public async deleteRepeat(
        @Body() requestBody: DeleteRepeatsParams
    ): Promise<ApiVoidResult | ApiValidationError> {
        try {
            await new RepeatsService().deleteRepeat(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("repeats/add-disabled-date")
@Middlewares(authenticateMiddleware)
export class AddRepeatDisabledDateController extends Controller {
    @Response<ApiValidationError>(500, "Ha ocurrido un error")
    @SuccessResponse(200, "Done")
    @Post()
    public async addRepeatDisabledDate(
        @Body() requestBody: AddRepeatDisabledDateParams
    ): Promise<ApiVoidResult | ApiValidationError> {
        try {
            await new RepeatsService().addRepeatDisabledDate(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("repeats/repeats-by-client")
export class GetRepeatsByClientController extends Controller {
    @Response<ApiValidationError>(500, "Ha ocurrido un error")
    @SuccessResponse(200, "Done")
    @Post()
    public async getRepeatsByClient(
        @Body() requestBody: FindRepeatsParams
    ): Promise<RepeatView[] | ApiValidationError> {
        try {
            const repeats = await new RepeatsService().getRepeatsByClient(requestBody);
            this.setStatus(200);
            return repeats;
        } catch (e) {
            const errorOccurred: Error = e as Error;
            this.setStatus(500);
            return new ApiValidationError(500, errorOccurred.message);
        }
    }
}

@Route("repeats/pending-current-week")
@Middlewares(authenticateMiddleware)
export class GetPendingRepeatsCurrentWeekController extends Controller {
    @Response<ApiValidationError>(500, "Ha ocurrido un error")
    @SuccessResponse(200, "Done")
    @Post()
    public async getPendingRepeatsCurrentWeek(
        @Body() requestBody: GetPendingRepeatsParams
    ): Promise<PendingRepeatView[] | ApiValidationError> {
        try {
            const repeats = await new RepeatsService().getPendingRepeatsCurrentWeek(requestBody);
            this.setStatus(200);
            return repeats;
        } catch (e) {
            const errorOccurred: Error = e as Error;
            this.setStatus(500);
            return new ApiValidationError(500, errorOccurred.message);
        }
    }
}
