// #region FeatureGateTest [C:2] [TYPE Module] [SEMANTICS test, ui, feature-gate, component, conditional]
// @BRIEF Unit tests for FeatureGate UI component — feature flag enable/disable, redirect, children rendering.
// @LAYER Tests
// @RELATION BINDS_TO -> [FeatureGate]
// @UX_STATE Enabled -> Renders children
// @UX_STATE Disabled -> Shows empty state or redirect link

import { describe, it, expect, vi } from 'vitest';
import { render, screen } from '@testing-library/svelte';
import FeatureGate from '../FeatureGate.svelte';

// Mock i18n — t must be a store with subscribe()
vi.mock('$lib/i18n/index.svelte.js', () => {
	const tStore = {
		subscribe: (run: (v: unknown) => void) => {
			run({
				common: {
					feature_disabled: 'Feature is disabled.',
					go_back: 'Go back',
				},
			});
			return () => {};
		},
	};
	return { t: tStore };
});

describe('FeatureGate', () => {
	it('renders children when feature is enabled', () => {
		const { container } = render(FeatureGate, {
			props: {
				feature: 'test_feature',
				features: { test_feature: true },
			},
		});
		// When enabled, the children snippet is rendered (even if empty)
		// The disabled message should not appear
		expect(container.textContent).not.toContain('Feature is disabled.');
		expect(container.textContent).not.toContain('Go back');
	});

	it('shows disabled message when feature is explicitly false', () => {
		const { container } = render(FeatureGate, {
			props: {
				feature: 'disabled_feature',
				features: { disabled_feature: false },
			},
		});
		expect(container.textContent).toContain('is disabled.');
	});

	it('shows redirect link when feature is disabled and redirect provided', () => {
		const { container } = render(FeatureGate, {
			props: {
				feature: 'gated_feature',
				features: { gated_feature: false },
				redirect: '/settings',
			},
		});
		expect(container.textContent).toContain('is disabled.');
		expect(container.textContent).toContain('Go back');
		const link = container.querySelector('a');
		expect(link).not.toBeNull();
		expect(link!.getAttribute('href')).toBe('/settings');
	});

	it('does not show link when disabled without redirect', () => {
		const { container } = render(FeatureGate, {
			props: {
				feature: 'gated_feature',
				features: { gated_feature: false },
			},
		});
		const link = container.querySelector('a');
		expect(link).toBeNull();
	});

	it('handles missing feature as enabled (undefined !== false)', () => {
		const { container } = render(FeatureGate, {
			props: {
				feature: 'new_feature',
				features: {},
			},
		});
		// Missing features are treated as enabled
		expect(container.textContent).not.toContain('Feature is disabled.');
	});

	it('handles explicitly undefined feature value as enabled', () => {
		const { container } = render(FeatureGate, {
			props: {
				feature: 'new_feature',
				features: { new_feature: undefined },
			},
		});
		// undefined !== false, so it's enabled
		expect(container.textContent).not.toContain('Feature is disabled.');
	});

	it('uses default empty features object when features prop is not provided', () => {
		// Line 15: features = $bindable({} as Record<string, boolean | undefined>)
		// When features is not passed, it defaults to {} and all features are enabled
		const { container } = render(FeatureGate, {
			props: {
				feature: 'unlisted_feature',
				// No features prop — should use default {}
			},
		});
		// With empty features default, any feature is enabled (undefined !== false)
		expect(container.textContent).not.toContain('Feature is disabled.');
	});

	it('passes default feature as empty string when feature prop is not provided', () => {
		const { container } = render(FeatureGate, {
			props: {
				features: { '': true },
			},
		});
		// feature defaults to "" — if features has "" as true, children render
		expect(container.textContent).not.toContain('Feature is disabled.');
	});

	it('passes default redirect as empty string when redirect prop is not provided', () => {
		const { container } = render(FeatureGate, {
			props: {
				feature: 'some_feature',
				features: { some_feature: false },
			},
		});
		// redirect defaults to "" — falsy — so no link is shown
		const link = container.querySelector('a');
		expect(link).toBeNull();
	});
});
// #endregion FeatureGateTest
