import { ApiValidationError } from "../../Models/Server.Error.model";
import { Body, Controller, Middlewares, Post, Response, Route, SuccessResponse } from "tsoa";
import { SystemNotificationsService } from "./SystemNotifications.Service";
import { authenticateMiddleware } from "../../middleware/authentication";
import {
    DeleteNotificationsByDateParams,
    DeleteSystemNotificationParams,
    FindSystemNotificationsParams,
    PaginateSystemNotificationsParams,
    PaginateSystemNotificationsResults,
    ToggleStateSystemNotificationsParams,
    UnreadNotificationResults,
    UpdateSystemNotificationParams,
} from "../../Models/SystemNotifications/SystemNotification.Interface";
import { ApiVoidResult } from "../../Models/Api.VoidResult.type";

@Route("systemnotifications/set-state")
@Middlewares(authenticateMiddleware)
export class SetStateByUserSystemNotificationsController extends Controller {
    @Response<ApiValidationError>(500, "Ha ocurrido un error")
    @SuccessResponse(200, "Done")
    @Post()
    public async setStateSystemNotificationsByUser(
        @Body() requestBody: ToggleStateSystemNotificationsParams
    ): Promise<ApiVoidResult | ApiValidationError> {
        try {
            await new SystemNotificationsService().setStateByUser(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("systemnotifications/paginate")
@Middlewares(authenticateMiddleware)
export class PaginateSystemNotificationsController extends Controller {
    @Response<ApiValidationError>(500, "Ha ocurrido un error")
    @SuccessResponse(200, "Done")
    @Post()
    public async paginateNotifications(
        @Body() requestBody: PaginateSystemNotificationsParams
    ): Promise<PaginateSystemNotificationsResults | ApiValidationError> {
        try {
            const notifications = await new SystemNotificationsService().paginateNotifications(requestBody);
            this.setStatus(200);
            return notifications;
        } catch (e) {
            const errorOccurred: Error = e as Error;
            this.setStatus(500);
            return new ApiValidationError(500, errorOccurred.message);
        }
    }
}

@Route("systemnotifications/unread")
@Middlewares(authenticateMiddleware)
export class UnreadSystemNotificationsController extends Controller {
    @Response<ApiValidationError>(500, "Ha ocurrido un error")
    @SuccessResponse(200, "Done")
    @Post()
    public async unreadNotifications(
        @Body() requestBody: FindSystemNotificationsParams
    ): Promise<UnreadNotificationResults | ApiValidationError> {
        try {
            const notifications = await new SystemNotificationsService().findUnreadNotifications(requestBody);
            this.setStatus(200);
            return notifications;
        } catch (e) {
            const errorOccurred: Error = e as Error;
            this.setStatus(500);
            return new ApiValidationError(500, errorOccurred.message);
        }
    }
}

@Route("systemnotifications/update")
@Middlewares(authenticateMiddleware)
export class UpdateSystemNotificationsController extends Controller {
    @Response<ApiValidationError>(500, "Ha ocurrido un error")
    @SuccessResponse(200, "Done")
    @Post()
    public async updateNotifications(
        @Body() requestBody: UpdateSystemNotificationParams
    ): Promise<ApiVoidResult | ApiValidationError> {
        try {
            await new SystemNotificationsService().updateNotification(requestBody);
            this.setStatus(200);
            return {
                success: true,
                message: "Notificación actualizada correctamente",
            };
        } catch (e) {
            const errorOccurred: Error = e as Error;
            this.setStatus(500);
            return new ApiValidationError(500, errorOccurred.message);
        }
    }
}

@Route("systemnotifications/delete")
@Middlewares(authenticateMiddleware)
export class DeleteSystemNotificationsController extends Controller {
    @Response<ApiValidationError>(500, "Ha ocurrido un error")
    @SuccessResponse(200, "Done")
    @Post()
    public async deleteNotifications(
        @Body() requestBody: DeleteSystemNotificationParams
    ): Promise<ApiVoidResult | ApiValidationError> {
        try {
            await new SystemNotificationsService().deleteNotification(requestBody);
            this.setStatus(200);
            return {
                success: true,
                message: "Notificación eliminada correctamente",
            };
        } catch (e) {
            const errorOccurred: Error = e as Error;
            this.setStatus(500);
            return new ApiValidationError(500, errorOccurred.message);
        }
    }
}

@Route("systemnotifications/delete-by-date")
@Middlewares(authenticateMiddleware)
export class DeleteSystemNotificationsByDateController extends Controller {
    @Response<ApiValidationError>(500, "Ha ocurrido un error")
    @SuccessResponse(200, "Done")
    @Post()
    public async deleteNotificationsByDate(
        @Body() requestBody: DeleteNotificationsByDateParams
    ): Promise<ApiVoidResult | ApiValidationError> {
        try {
            await new SystemNotificationsService().deleteNotificationsByDate(requestBody);
            this.setStatus(200);
            return {
                success: true,
                message: "Notificaciones eliminadas correctamente",
            };
        } catch (e) {
            const errorOccurred: Error = e as Error;
            this.setStatus(500);
            return new ApiValidationError(500, errorOccurred.message);
        }
    }
}
