// @vitest-environment jsdom
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { render, screen, waitFor } from '@testing-library/react';
import { ProductAccessibilityGate } from './ProductAccessibilityGate';

const { checkMock } = vi.hoisted(() => ({
    checkMock: vi.fn()
}));

vi.mock('../lib/runtime', async () => {
    const actual = await vi.importActual('../lib/runtime');
    return {
        ...actual,
        tradingKillSwitchClient: {
            check: checkMock
        }
    };
});

describe('ProductAccessibilityGate smoke', () => {
    beforeEach(() => {
        checkMock.mockReset();
    });

    it('renders children when product is available', async () => {
        checkMock.mockResolvedValue({ disabled: false });

        render(
            <ProductAccessibilityGate>
                <div>workspace-loaded</div>
            </ProductAccessibilityGate>
        );

        await waitFor(() => {
            expect(screen.getByText('workspace-loaded')).toBeInTheDocument();
        });
    });

    it('blocks workspace when control plane disables the product', async () => {
        checkMock.mockResolvedValue({
            disabled: true,
            message: 'Trading access disabled by control plane'
        });

        render(
            <ProductAccessibilityGate>
                <div>workspace-loaded</div>
            </ProductAccessibilityGate>
        );

        await waitFor(() => {
            expect(screen.getByText('Trading temporarily unavailable')).toBeInTheDocument();
            expect(screen.getByText('Trading access disabled by control plane')).toBeInTheDocument();
        });
        expect(screen.queryByText('workspace-loaded')).not.toBeInTheDocument();
    });
});
