import { Controller, Post, Route, SuccessResponse, Body, Middlewares } from "tsoa";
import { ApiVoidResult } from "../../Models/Api.VoidResult.type";
import { ApiValidationError } from "../../Models/Server.Error.model";
import { ErrorReportParams } from "../../Models/ErrorReporting/ErrorReporting";
import { ErrorReportingService } from "./ErrorReporting.Service";
import { rateLimiter } from "../../middleware/ratelimiter";

@Route("error-reporting")
export class ErrorReportingController extends Controller {
    @SuccessResponse(201, "Created")
    @Post("report")
    @Middlewares(rateLimiter)
    public async reportError(
        @Body() requestBody: ErrorReportParams
    ): Promise<ApiVoidResult | ApiValidationError> {
        try {
            await new ErrorReportingService().report(requestBody);
            this.setStatus(201);
            return { success: true, message: "Error reported successfully." };
        } catch (e) {
            const errorOccurred: Error = e as Error;
            this.setStatus(500);
            return new ApiValidationError(500, errorOccurred.message);
        }
    }
}
