import { ApiError } from '../../common/errors/ApiError.js'; import { subscriptionsRepository } from './subscriptions.repository.js'; const addDays = (date, days) => { const next = new Date(date); next.setDate(next.getDate() + days); return next; }; const calculateDiscountedPrice = (plan, member) => { const planPrice = Number(plan.price); const percentDiscount = member.discountPercent ? (planPrice * Number(member.discountPercent)) / 100 : 0; const fixedDiscount = Number(member.discountAmount || 0); const discountAmount = Math.min(planPrice, percentDiscount + fixedDiscount); return { discountAmount, finalPrice: Math.max(planPrice - discountAmount, 0), }; }; export const subscriptionsService = { async assign(gymId, { memberId, planId, startDate = new Date() }) { const plan = await subscriptionsRepository.findPlan(gymId, planId); if (!plan) throw ApiError.notFound('Subscription plan not found'); const member = await subscriptionsRepository.findMember(gymId, memberId); if (!member) throw ApiError.notFound('Member not found'); const billing = calculateDiscountedPrice(plan, member); return subscriptionsRepository.createActiveWithPayment({ gymId, memberId, planId, startDate, endDate: addDays(startDate, plan.durationDays), discountAmount: billing.discountAmount, finalPrice: billing.finalPrice, paymentMethod: 'cash', notes: billing.discountAmount > 0 ? `Subscription payment after EGP ${billing.discountAmount} discount` : 'Subscription payment', }); }, async purchaseForMember(gymId, memberId, { planId, paymentMethod, notes }) { const plan = await subscriptionsRepository.findPlan(gymId, planId); if (!plan) throw ApiError.notFound('Subscription plan not found'); const member = await subscriptionsRepository.findMember(gymId, memberId); if (!member) throw ApiError.notFound('Member not found'); const startDate = new Date(); const billing = calculateDiscountedPrice(plan, member); const paymentLabels = { cash: 'Cash', card: 'Visa', wallet: 'Vodafone Cash', }; return subscriptionsRepository.createMemberPurchase({ gymId, memberId, planId, startDate, endDate: addDays(startDate, plan.durationDays), discountAmount: billing.discountAmount, finalPrice: billing.finalPrice, paymentMethod, notes: notes || `${paymentLabels[paymentMethod]} membership checkout pending admin confirmation`, }); }, async renew(gymId, subscriptionId, payload) { const subscription = await subscriptionsRepository.findById(gymId, subscriptionId); if (!subscription) throw ApiError.notFound('Subscription not found'); const planId = payload.planId || subscription.planId; const plan = await subscriptionsRepository.findPlan(gymId, planId); if (!plan) throw ApiError.notFound('Subscription plan not found'); const member = await subscriptionsRepository.findMember(gymId, subscription.memberId); if (!member) throw ApiError.notFound('Member not found'); const startDate = payload.startDate || new Date(); const billing = calculateDiscountedPrice(plan, member); return subscriptionsRepository.renewWithPayment(gymId, subscriptionId, { planId, startDate, endDate: addDays(startDate, plan.durationDays), status: 'active', freezeDays: 0, discountAmount: billing.discountAmount, finalPrice: billing.finalPrice, }, { amount: billing.finalPrice, paymentMethod: payload.paymentMethod || 'cash', notes: billing.discountAmount > 0 ? `Renewal payment after EGP ${billing.discountAmount} discount` : 'Renewal payment', }); }, async freeze(gymId, subscriptionId, freezeDays) { const subscription = await subscriptionsRepository.findById(gymId, subscriptionId); if (!subscription) throw ApiError.notFound('Subscription not found'); return subscriptionsRepository.update(gymId, subscriptionId, { status: 'frozen', freezeDays, endDate: addDays(subscription.endDate, freezeDays), }); }, };