// SPDX-License-Identifier: AGPL-3.0-or-later

import {UserPremiumTypes} from '@fluxer/constants/src/UserConstants';
import {Config} from '../Config';
import type {Currency} from '../utils/CurrencyUtils';

export enum ProductType {
	MONTHLY_SUBSCRIPTION = 'monthly_subscription',
	YEARLY_SUBSCRIPTION = 'yearly_subscription',
	GIFT_1_MONTH = 'gift_1_month',
	GIFT_1_YEAR = 'gift_1_year',
}

export type RecurringBillingCycle = 'monthly' | 'yearly';

export interface ProductInfo {
	type: ProductType;
	premiumType: 1 | 2;
	durationMonths: number;
	isGift: boolean;
	currency: Currency;
	billingCycle?: RecurringBillingCycle;
}

export class ProductRegistry {
	private products = new Map<string, ProductInfo>();

	constructor() {
		const prices = Config.stripe.prices;
		if (!prices) return;
		this.registerProduct(prices.monthlyUsd, {
			type: ProductType.MONTHLY_SUBSCRIPTION,
			premiumType: UserPremiumTypes.SUBSCRIPTION,
			durationMonths: 1,
			isGift: false,
			currency: 'USD',
			billingCycle: 'monthly',
		});
		this.registerProduct(prices.monthlyEur, {
			type: ProductType.MONTHLY_SUBSCRIPTION,
			premiumType: UserPremiumTypes.SUBSCRIPTION,
			durationMonths: 1,
			isGift: false,
			currency: 'EUR',
			billingCycle: 'monthly',
		});
		this.registerProduct(prices.monthlyBrl, {
			type: ProductType.MONTHLY_SUBSCRIPTION,
			premiumType: UserPremiumTypes.SUBSCRIPTION,
			durationMonths: 1,
			isGift: false,
			currency: 'BRL',
			billingCycle: 'monthly',
		});
		this.registerProduct(prices.monthlyInr, {
			type: ProductType.MONTHLY_SUBSCRIPTION,
			premiumType: UserPremiumTypes.SUBSCRIPTION,
			durationMonths: 1,
			isGift: false,
			currency: 'INR',
			billingCycle: 'monthly',
		});
		this.registerProduct(prices.monthlyPln, {
			type: ProductType.MONTHLY_SUBSCRIPTION,
			premiumType: UserPremiumTypes.SUBSCRIPTION,
			durationMonths: 1,
			isGift: false,
			currency: 'PLN',
			billingCycle: 'monthly',
		});
		this.registerProduct(prices.monthlyTry, {
			type: ProductType.MONTHLY_SUBSCRIPTION,
			premiumType: UserPremiumTypes.SUBSCRIPTION,
			durationMonths: 1,
			isGift: false,
			currency: 'TRY',
			billingCycle: 'monthly',
		});
		this.registerProduct(prices.yearlyUsd, {
			type: ProductType.YEARLY_SUBSCRIPTION,
			premiumType: UserPremiumTypes.SUBSCRIPTION,
			durationMonths: 12,
			isGift: false,
			currency: 'USD',
			billingCycle: 'yearly',
		});
		this.registerProduct(prices.yearlyEur, {
			type: ProductType.YEARLY_SUBSCRIPTION,
			premiumType: UserPremiumTypes.SUBSCRIPTION,
			durationMonths: 12,
			isGift: false,
			currency: 'EUR',
			billingCycle: 'yearly',
		});
		this.registerProduct(prices.yearlyBrl, {
			type: ProductType.YEARLY_SUBSCRIPTION,
			premiumType: UserPremiumTypes.SUBSCRIPTION,
			durationMonths: 12,
			isGift: false,
			currency: 'BRL',
			billingCycle: 'yearly',
		});
		this.registerProduct(prices.yearlyInr, {
			type: ProductType.YEARLY_SUBSCRIPTION,
			premiumType: UserPremiumTypes.SUBSCRIPTION,
			durationMonths: 12,
			isGift: false,
			currency: 'INR',
			billingCycle: 'yearly',
		});
		this.registerProduct(prices.yearlyPln, {
			type: ProductType.YEARLY_SUBSCRIPTION,
			premiumType: UserPremiumTypes.SUBSCRIPTION,
			durationMonths: 12,
			isGift: false,
			currency: 'PLN',
			billingCycle: 'yearly',
		});
		this.registerProduct(prices.yearlyTry, {
			type: ProductType.YEARLY_SUBSCRIPTION,
			premiumType: UserPremiumTypes.SUBSCRIPTION,
			durationMonths: 12,
			isGift: false,
			currency: 'TRY',
			billingCycle: 'yearly',
		});
		this.registerProduct(prices.gift1MonthUsd, {
			type: ProductType.GIFT_1_MONTH,
			premiumType: UserPremiumTypes.SUBSCRIPTION,
			durationMonths: 1,
			isGift: true,
			currency: 'USD',
		});
		this.registerProduct(prices.gift1MonthEur, {
			type: ProductType.GIFT_1_MONTH,
			premiumType: UserPremiumTypes.SUBSCRIPTION,
			durationMonths: 1,
			isGift: true,
			currency: 'EUR',
		});
		this.registerProduct(prices.gift1MonthBrl, {
			type: ProductType.GIFT_1_MONTH,
			premiumType: UserPremiumTypes.SUBSCRIPTION,
			durationMonths: 1,
			isGift: true,
			currency: 'BRL',
		});
		this.registerProduct(prices.gift1MonthInr, {
			type: ProductType.GIFT_1_MONTH,
			premiumType: UserPremiumTypes.SUBSCRIPTION,
			durationMonths: 1,
			isGift: true,
			currency: 'INR',
		});
		this.registerProduct(prices.gift1MonthPln, {
			type: ProductType.GIFT_1_MONTH,
			premiumType: UserPremiumTypes.SUBSCRIPTION,
			durationMonths: 1,
			isGift: true,
			currency: 'PLN',
		});
		this.registerProduct(prices.gift1MonthTry, {
			type: ProductType.GIFT_1_MONTH,
			premiumType: UserPremiumTypes.SUBSCRIPTION,
			durationMonths: 1,
			isGift: true,
			currency: 'TRY',
		});
		this.registerProduct(prices.gift1YearUsd, {
			type: ProductType.GIFT_1_YEAR,
			premiumType: UserPremiumTypes.SUBSCRIPTION,
			durationMonths: 12,
			isGift: true,
			currency: 'USD',
		});
		this.registerProduct(prices.gift1YearEur, {
			type: ProductType.GIFT_1_YEAR,
			premiumType: UserPremiumTypes.SUBSCRIPTION,
			durationMonths: 12,
			isGift: true,
			currency: 'EUR',
		});
		this.registerProduct(prices.gift1YearBrl, {
			type: ProductType.GIFT_1_YEAR,
			premiumType: UserPremiumTypes.SUBSCRIPTION,
			durationMonths: 12,
			isGift: true,
			currency: 'BRL',
		});
		this.registerProduct(prices.gift1YearInr, {
			type: ProductType.GIFT_1_YEAR,
			premiumType: UserPremiumTypes.SUBSCRIPTION,
			durationMonths: 12,
			isGift: true,
			currency: 'INR',
		});
		this.registerProduct(prices.gift1YearPln, {
			type: ProductType.GIFT_1_YEAR,
			premiumType: UserPremiumTypes.SUBSCRIPTION,
			durationMonths: 12,
			isGift: true,
			currency: 'PLN',
		});
		this.registerProduct(prices.gift1YearTry, {
			type: ProductType.GIFT_1_YEAR,
			premiumType: UserPremiumTypes.SUBSCRIPTION,
			durationMonths: 12,
			isGift: true,
			currency: 'TRY',
		});
	}

	private registerProduct(priceId: string | undefined, info: ProductInfo): void {
		if (priceId) {
			this.products.set(priceId, info);
		}
	}

	getProduct(priceId: string): ProductInfo | null {
		return this.products.get(priceId) || null;
	}

	isRecurringSubscription(info: ProductInfo): boolean {
		return !info.isGift && info.premiumType === UserPremiumTypes.SUBSCRIPTION;
	}

	getRecurringSubscriptionPriceId(billingCycle: RecurringBillingCycle, currency: string): string | null {
		const normalizedCurrency = currency.trim().toLowerCase();
		const prices = Config.stripe.prices;
		if (!prices) {
			return null;
		}
		if (normalizedCurrency === 'eur') {
			return billingCycle === 'monthly' ? (prices.monthlyEur ?? null) : (prices.yearlyEur ?? null);
		}
		if (normalizedCurrency === 'brl') {
			return billingCycle === 'monthly' ? (prices.monthlyBrl ?? null) : (prices.yearlyBrl ?? null);
		}
		if (normalizedCurrency === 'inr') {
			return billingCycle === 'monthly' ? (prices.monthlyInr ?? null) : (prices.yearlyInr ?? null);
		}
		if (normalizedCurrency === 'pln') {
			return billingCycle === 'monthly' ? (prices.monthlyPln ?? null) : (prices.yearlyPln ?? null);
		}
		if (normalizedCurrency === 'try') {
			return billingCycle === 'monthly' ? (prices.monthlyTry ?? null) : (prices.yearlyTry ?? null);
		}
		if (normalizedCurrency === 'usd') {
			return billingCycle === 'monthly' ? (prices.monthlyUsd ?? null) : (prices.yearlyUsd ?? null);
		}
		return null;
	}

	getGiftPriceId(duration: 'gift_1_month' | 'gift_1_year', currency: string): string | null {
		const normalizedCurrency = currency.trim().toLowerCase();
		const prices = Config.stripe.prices;
		if (!prices) {
			return null;
		}
		if (normalizedCurrency === 'eur') {
			return duration === 'gift_1_month' ? (prices.gift1MonthEur ?? null) : (prices.gift1YearEur ?? null);
		}
		if (normalizedCurrency === 'brl') {
			return duration === 'gift_1_month' ? (prices.gift1MonthBrl ?? null) : (prices.gift1YearBrl ?? null);
		}
		if (normalizedCurrency === 'inr') {
			return duration === 'gift_1_month' ? (prices.gift1MonthInr ?? null) : (prices.gift1YearInr ?? null);
		}
		if (normalizedCurrency === 'pln') {
			return duration === 'gift_1_month' ? (prices.gift1MonthPln ?? null) : (prices.gift1YearPln ?? null);
		}
		if (normalizedCurrency === 'try') {
			return duration === 'gift_1_month' ? (prices.gift1MonthTry ?? null) : (prices.gift1YearTry ?? null);
		}
		if (normalizedCurrency === 'usd') {
			return duration === 'gift_1_month' ? (prices.gift1MonthUsd ?? null) : (prices.gift1YearUsd ?? null);
		}
		return null;
	}
}
