import { IsBillingEnabled } from "../EnvironmentConfig";
import UserMiddleware from "../Middleware/UserAuthorization";
import BillingPaymentMethodService from "../Services/BillingPaymentMethodService";
import BillingService from "../Services/BillingService";
import ProjectService from "../Services/ProjectService";
import Response from "../Utils/Response";
import BaseAPI from "./BaseAPI";
import BadDataException from "../../Types/Exception/BadDataException";
import Permission from "../../Types/Permission";
import BillingPaymentMethod from "../../Models/DatabaseModels/BillingPaymentMethod";
export default class UserAPI extends BaseAPI {
    constructor() {
        var _a;
        super(BillingPaymentMethod, BillingPaymentMethodService);
        this.router.post(`${(_a = new this.entityType().getCrudApiPath()) === null || _a === void 0 ? void 0 : _a.toString()}/setup`, UserMiddleware.getUserMiddleware, async (req, res, next) => {
            try {
                if (!IsBillingEnabled) {
                    throw new BadDataException("Billing is not enabled for this server");
                }
                if (req.body["projectId"]) {
                    throw new BadDataException("projectId is required in request body");
                }
                const userPermissions = (await this.getPermissionsForTenant(req)).filter((permission) => {
                    return [
                        Permission.ProjectOwner,
                        Permission.ManageProjectBilling,
                        Permission.CreateBillingPaymentMethod,
                    ].includes(permission.permission);
                });
                if (userPermissions.length === 0) {
                    throw new BadDataException("Only project owners or members with Manage Billing access can add payment methods.");
                }
                const project = await ProjectService.findOneById({
                    id: this.getTenantId(req),
                    props: {
                        isRoot: true,
                    },
                    select: {
                        _id: true,
                        paymentProviderCustomerId: true,
                    },
                });
                if (!project) {
                    throw new BadDataException("Project not found");
                }
                if (!project) {
                    throw new BadDataException("Project not found");
                }
                if (!project.paymentProviderCustomerId) {
                    throw new BadDataException("Payment Provider customer not found");
                }
                const setupIntent = await BillingService.getSetupIntentSecret(project.paymentProviderCustomerId);
                return Response.sendJsonObjectResponse(req, res, {
                    setupIntent: setupIntent,
                });
            }
            catch (err) {
                next(err);
            }
        });
    }
}
//# sourceMappingURL=BillingPaymentMethodAPI.js.map