import { FindPlansParams, IPlan } from "../../Models/Plans/Plans.interface";
import { ApiValidationError } from "../../Models/Server.Error.model";
import { Body, Controller, Post, Response, Route, SuccessResponse } from "tsoa";
import { PlanService } from "./Plans.Service";

@Route("plans/find")
export class PlansFindController extends Controller {
    @Response<ApiValidationError>(500, "Ha ocurrido un error")
    @SuccessResponse(200, "Done")
    @Post()
    public async findPlans(
        @Body() requestBody: FindPlansParams
    ): Promise<IPlan[] | ApiValidationError> {
        try {
            const plans = await new PlanService().findPlans(requestBody);
            this.setStatus(200);
            return plans;
        } catch (e) {
            const errorOccurred: Error = e as Error;
            this.setStatus(500);
            return new ApiValidationError(500, errorOccurred.message);
        }
    }
}
