'use strict';
import { createDynamicSelect, dbWhere, dbUpdate, dbInnerJoin } from '../../../db/db-connection.js';
import { getTableColumn, runQuery } from '../../../db/drizzle-queries.js';
import { getDbSchema } from '../../../db/db-schema.js';
import { eq, and, isNull, isNotNull, lt } from 'drizzle-orm';
import { getNowUtc } from '../../../../fd-toolbox/dates/date-time.js';
import { subscriptionStatuses } from '../../../meta/core/resources/enums/payment-enums.js';
import { resourceNames } from '../../../../fd-toolbox/resources/resource-names.js';
import { createResource } from '../../../meta/server/resources/crud-resources.js';
import { schemaNames, columnNames } from '../../../db/constants/schema-names.js';
import { singleOrUndefined } from '../../../../fd-toolbox/server/collections/single-funcs.js';

var __defProp=Object['defineProperty'],__name=(_0x30ae4a,_0x11991f)=>__defProp(_0x30ae4a,'name',{'value':_0x11991f,'configurable':!![]});async function createSubscription(_0x305099){return createResource(_0x305099,resourceNames['subscription']);}__name(createSubscription,'createSubscription');async function getOneSubscription(_0x5480c0){const _0x142a89=getDbSchema(),_0x4b8cb9=_0x142a89[schemaNames['subscriptionSchema']],_0x498875=createDynamicSelect(_0x4b8cb9);dbWhere(_0x498875,eq(getTableColumn(_0x4b8cb9,columnNames['workspaceId']),_0x5480c0));const _0x577b73=await runQuery(_0x498875);return singleOrUndefined(_0x577b73);}__name(getOneSubscription,'getOneSubscription');async function getSubscriptionsByWorkspaceId(_0x1795da){const _0x5c9ae2=getDbSchema(),_0x960c75=_0x5c9ae2[schemaNames['subscriptionSchema']],_0x73ffdb=createDynamicSelect(_0x960c75);return dbWhere(_0x73ffdb,eq(getTableColumn(_0x960c75,columnNames['workspaceId']),_0x1795da)),runQuery(_0x73ffdb);}__name(getSubscriptionsByWorkspaceId,'getSubscriptionsByWorkspaceId');async function getFreeSubscriptionByWorkspaceId(_0x521979){const _0x2685c6=getDbSchema(),_0x52a151=_0x2685c6[schemaNames['subscriptionSchema']],_0x9aee3e=createDynamicSelect(_0x52a151);dbWhere(_0x9aee3e,and(eq(getTableColumn(_0x52a151,columnNames['workspaceId']),_0x521979),isNull(getTableColumn(_0x52a151,columnNames['paymentSystemSubscriptionId']))));const _0x53d0c8=await runQuery(_0x9aee3e);return singleOrUndefined(_0x53d0c8);}__name(getFreeSubscriptionByWorkspaceId,'getFreeSubscriptionByWorkspaceId');async function changeSubscriptionStatusById(_0x521c96,_0x434b85){const _0x20407a=getDbSchema(),_0x2bc7c0=_0x20407a[schemaNames['subscriptionSchema']],_0x28af09={'status':_0x434b85};await dbUpdate(_0x2bc7c0)['set'](_0x28af09)['where'](eq(getTableColumn(_0x2bc7c0,columnNames['id']),_0x521c96));const _0x99509a=await selectSubscription(eq(getTableColumn(_0x2bc7c0,columnNames['id']),_0x521c96));return singleOrUndefined(_0x99509a);}__name(changeSubscriptionStatusById,'changeSubscriptionStatusById');async function selectSubscription(_0x12ee49){const _0x11710c=getDbSchema(),_0xdca978=_0x11710c[schemaNames['subscriptionSchema']],_0x401ab1=_0x11710c[schemaNames['pricingSchema']],_0x1d509a=_0x11710c[schemaNames['paymentPlanSchema']],_0x3bda75=createDynamicSelect(_0xdca978,{'id':getTableColumn(_0xdca978,columnNames['id']),'paymentPlanName':getTableColumn(_0x1d509a,columnNames['name']),'status':getTableColumn(_0xdca978,columnNames['status']),'currentPeriodEndDate':getTableColumn(_0xdca978,columnNames['currentPeriodEndDate']),'endDate':getTableColumn(_0xdca978,columnNames['endDate']),'price':getTableColumn(_0x401ab1,columnNames['price']),'currency':getTableColumn(_0x401ab1,columnNames['currency']),'billingInterval':getTableColumn(_0x401ab1,columnNames['billingInterval']),'paymentSystemSubscriptionId':getTableColumn(_0xdca978,columnNames['paymentSystemSubscriptionId'])});return dbInnerJoin(_0x3bda75,_0x401ab1,eq(getTableColumn(_0xdca978,columnNames['pricingId']),getTableColumn(_0x401ab1,columnNames['id']))),dbInnerJoin(_0x3bda75,_0x1d509a,eq(getTableColumn(_0x401ab1,columnNames['paymentPlanId']),getTableColumn(_0x1d509a,columnNames['id']))),dbWhere(_0x3bda75,_0x12ee49),runQuery(_0x3bda75);}__name(selectSubscription,'selectSubscription');async function getSubscriptionsDueForCheck(){const _0x267c19=getDbSchema(),_0x1d26cb=_0x267c19[schemaNames['subscriptionSchema']],_0x325f2b=createDynamicSelect(_0x1d26cb);return dbWhere(_0x325f2b,and(isNotNull(getTableColumn(_0x1d26cb,columnNames['paymentSystemSubscriptionId'])),eq(getTableColumn(_0x1d26cb,columnNames['status']),subscriptionStatuses['active']),lt(getTableColumn(_0x1d26cb,columnNames['currentPeriodEndDate']),getNowUtc()))),runQuery(_0x325f2b);}__name(getSubscriptionsDueForCheck,'getSubscriptionsDueForCheck');async function getSubscriptionByPaymentSystemSubscrId(_0x25fa2e){const _0x36ad17=getDbSchema(),_0xa3b40a=_0x36ad17[schemaNames['subscriptionSchema']],_0x42cce0=await selectSubscription(eq(getTableColumn(_0xa3b40a,columnNames['paymentSystemSubscriptionId']),_0x25fa2e));return singleOrUndefined(_0x42cce0);}__name(getSubscriptionByPaymentSystemSubscrId,'getSubscriptionByPaymentSystemSubscrId');

export { changeSubscriptionStatusById, createSubscription, getFreeSubscriptionByWorkspaceId, getOneSubscription, getSubscriptionByPaymentSystemSubscrId, getSubscriptionsByWorkspaceId, getSubscriptionsDueForCheck, selectSubscription };
