/**
 * Plan comparison rows — copy of https://buildler.com/booking/pricing
 * (“Every feature, by plan”). Keep in lockstep with that table.
 *
 * @package
 */

export type PlanSlug = 'free' | 'pro' | 'business' | 'ultimate';

export const PLAN_SLUGS: readonly PlanSlug[] = [
	'free',
	'pro',
	'business',
	'ultimate',
];

export const PLAN_LABELS: Record< PlanSlug, string > = {
	free: 'Free',
	pro: 'Pro',
	business: 'Business',
	ultimate: 'Ultimate',
};

export type CellValue = boolean | string;

export type PlanMatrixRow =
	| {
			readonly kind: 'section';
			readonly id: string;
			readonly label: string;
	  }
	| {
			readonly kind: 'feature';
			readonly id: string;
			readonly label: string;
			readonly cells: Record< PlanSlug, CellValue >;
	  };

const yes = true;
const no = false;
const unlimited = 'Unlimited';

function section( id: string, label: string ): PlanMatrixRow {
	return { kind: 'section', id, label };
}

function feature(
	id: string,
	label: string,
	free: CellValue,
	pro: CellValue,
	business: CellValue,
	ultimate: CellValue
): PlanMatrixRow {
	return {
		kind: 'feature',
		id,
		label,
		cells: { free, pro, business, ultimate },
	};
}

export const PLAN_ROWS: readonly PlanMatrixRow[] = [
	section( 'booking', 'Booking' ),
	feature(
		'appointments',
		'Appointments per month',
		unlimited,
		unlimited,
		unlimited,
		unlimited
	),
	feature( 'services', 'Services', '7', unlimited, unlimited, unlimited ),
	feature( 'staff', 'Staff members', '3', unlimited, unlimited, unlimited ),
	feature( 'locations', 'Locations', '2', unlimited, unlimited, unlimited ),
	feature(
		'booking_forms',
		'Booking forms',
		'2',
		unlimited,
		unlimited,
		unlimited
	),
	feature( 'group_bookings', 'Group bookings', no, no, yes, yes ),
	feature( 'recurring', 'Recurring appointments', no, no, yes, yes ),
	feature( 'waitlist', 'Waiting lists', no, no, yes, yes ),
	feature(
		'extras_packages',
		'Packages and service extras',
		no,
		no,
		yes,
		yes
	),
	feature(
		'chained_services',
		'Chained and compound services',
		no,
		no,
		no,
		yes
	),
	section( 'payments', 'Payments' ),
	feature( 'pay_on_site', 'Pay on site', yes, yes, yes, yes ),
	feature( 'stripe_paypal', 'Stripe and PayPal', no, yes, yes, yes ),
	feature( 'deposits', 'Deposits and balances', no, yes, yes, yes ),
	feature( 'coupons', 'Coupons', no, yes, yes, yes ),
	feature( 'refunds_admin', 'Refunds from the admin', no, yes, yes, yes ),
	feature( 'all_gateways', 'Every gateway we support', no, no, no, yes ),
	section( 'notifications', 'Notifications' ),
	feature( 'email_confirmations', 'Email confirmations', yes, yes, yes, yes ),
	feature(
		'reminders',
		'Reminders per booking',
		unlimited,
		unlimited,
		unlimited,
		unlimited
	),
	feature(
		'notification_log',
		'Notification log and retry',
		yes,
		yes,
		yes,
		yes
	),
	feature( 'sms_whatsapp', 'SMS and WhatsApp', no, no, yes, yes ),
	feature(
		'automations',
		'Automations',
		'Email only',
		'Email only',
		'Full set',
		'Full set'
	),
	section( 'calendars', 'Calendars and meetings' ),
	feature( 'google_calendar', 'Two-way Google Calendar', no, yes, yes, yes ),
	feature( 'outlook_apple_calendar', 'Outlook and Apple', no, no, yes, yes ),
	feature( 'meetings', 'Zoom, Meet and Teams', no, no, yes, yes ),
	section( 'admin', 'Admin and reporting' ),
	feature(
		'customer_cabinet',
		'Customer self-service cabinet',
		no,
		yes,
		yes,
		yes
	),
	feature( 'custom_fields', 'Custom fields', no, yes, yes, yes ),
	feature( 'reports', 'Financial reports and CSV', no, no, yes, yes ),
	feature(
		'custom_statuses',
		'Custom statuses and customer groups',
		no,
		no,
		no,
		yes
	),
	section( 'developers', 'Developers and agencies' ),
	feature( 'rest_api', 'REST API and webhooks', yes, yes, yes, yes ),
	feature( 'ai_read', 'AI abilities (read)', yes, yes, yes, yes ),
	feature( 'ai_write_mcp', 'AI abilities (write) and MCP', no, no, no, yes ),
	feature( 'multisite', 'Multisite network management', no, no, no, yes ),
	feature(
		'white_label',
		'White-label and client-safe admin',
		no,
		no,
		no,
		yes
	),
	feature(
		'sites_per_licence',
		'Sites per licence',
		'1',
		'1',
		'1 or 5',
		'1, 20 or 100'
	),
];

export function parsePlanSlug( value: string ): PlanSlug {
	if (
		value === 'pro' ||
		value === 'business' ||
		value === 'ultimate' ||
		value === 'free'
	) {
		return value;
	}
	return 'free';
}

export function planRank( slug: PlanSlug ): number {
	return PLAN_SLUGS.indexOf( slug );
}

export type PlanMove = 'current' | 'upgrade' | 'downgrade';

export function planMove( current: PlanSlug, column: PlanSlug ): PlanMove {
	if ( current === column ) {
		return 'current';
	}
	return planRank( column ) > planRank( current ) ? 'upgrade' : 'downgrade';
}

export function formatCell( value: CellValue ): string {
	if ( value === true ) {
		return 'Included';
	}
	if ( value === false ) {
		return 'Not included';
	}
	return value;
}

export function featureCells(
	id: string
): Record< PlanSlug, CellValue > | null {
	const row = PLAN_ROWS.find( ( r ) => r.kind === 'feature' && r.id === id );
	return row && row.kind === 'feature' ? row.cells : null;
}
