import { ApiValidationError } from "../../Models/Server.Error.model";
import { Middlewares, Body, Controller, Post, Response, Route, SuccessResponse } from "tsoa";
import { PlanSubscriptionsService } from "./PlanSubscriptions.Service";
import {
    CancellPlanSuscriptionParams,
    CreateMPPreApprovalParams,
    GetSubscriptionInitPointParams,
    GetSuscriptionInitPointResponse,
    MPPreApprovalResponse,
} from "../../Models/PlanSubscriptions/PlanSubscriptions.interface";
import { authenticateMiddleware } from "../../middleware/authentication";
import { ApiVoidResult } from "../../Models/Api.VoidResult.type";

@Route("suscriptions/get-init-point")
@Middlewares(authenticateMiddleware)
export class PlanSubscriptionsGetInitPointController extends Controller {
    @Response<ApiValidationError>(500, "Ha ocurrido un error")
    @SuccessResponse(200, "Done")
    @Post()
    public async getPlanSubscriptionInitPoint(
        @Body() requestBody: GetSubscriptionInitPointParams
    ): Promise<GetSuscriptionInitPointResponse | ApiValidationError> {
        try {
            const result = await new PlanSubscriptionsService().getInitPoint(requestBody);
            this.setStatus(200);
            return result;
        } catch (e) {
            const errorOccurred: Error = e as Error;
            this.setStatus(500);
            return new ApiValidationError(500, errorOccurred.message);
        }
    }
}

@Route("suscriptions/preapproval")
@Middlewares(authenticateMiddleware)
export class PlanSubscriptionsCreatePreApprovalController extends Controller {
    @Response<ApiValidationError>(500, "Ha ocurrido un error")
    @SuccessResponse(200, "Done")
    @Post()
    public async createPlanSubscription(
        @Body() requestBody: CreateMPPreApprovalParams
    ): Promise<MPPreApprovalResponse | ApiValidationError> {
        try {
            const preApproval = await new PlanSubscriptionsService().create(requestBody);
            this.setStatus(200);
            return preApproval;
        } catch (e) {
            const errorOccurred: Error = e as Error;
            this.setStatus(500);
            return new ApiValidationError(500, errorOccurred.message);
        }
    }
}

@Route("suscriptions/cancell")
@Middlewares(authenticateMiddleware)
export class PlanSubscriptionsCancellController extends Controller {
    @Response<ApiValidationError>(500, "Ha ocurrido un error")
    @SuccessResponse(200, "Done")
    @Post()
    public async cancellPlanSubscription(
        @Body() requestBody: CancellPlanSuscriptionParams
    ): Promise<ApiVoidResult | ApiValidationError> {
        try {
            await new PlanSubscriptionsService().cancell(requestBody);
            return {
                message: "Done",
                success: true,
            };
        } catch (e) {
            const errorOccurred: Error = e as Error;
            this.setStatus(500);
            return new ApiValidationError(500, errorOccurred.message);
        }
    }
}
