// @vitest-environment jsdom
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { MemoryRouter } from 'react-router-dom';
import { describe, expect, it, vi } from 'vitest';
import { PortfolioView } from './PortfolioView';

const navigateMock = vi.fn();

vi.mock('react-router-dom', async () => {
  const actual = await vi.importActual<typeof import('react-router-dom')>('react-router-dom');
  return {
    ...actual,
    useNavigate: () => navigateMock,
  };
});

vi.mock('../context/AppContext', () => ({
  useAppContext: () => ({
    botState: {
      symbols: {},
      alerts: [],
      positions: [],
      orders: [],
      history: [],
      settings: {
        executionMode: 'Paper',
        riskPerTrade: 0.01,
        totalCapital: 1000,
        maxOpenTrades: 3,
        isAlgoEnabled: true,
        enabledRules: [],
      },
      uptime: 1000,
    },
  }),
}));

vi.mock('../tabs/PositionsTab', () => ({
  PositionsTab: ({ onManageHolding }: any) => (
    <div>
      <button
        type="button"
        onClick={() => onManageHolding?.({
          symbol: 'AAPL',
          tradeId: 'TRD-AAPL',
          planEntryId: 'setup-aapl',
        }, 'open-plan')}
      >
        open linked plan
      </button>
      <button
        type="button"
        onClick={() => onManageHolding?.({
          symbol: 'MSFT',
          tradeId: 'TRD-MSFT',
        }, 'create-exit-plan')}
      >
        create exit plan
      </button>
    </div>
  ),
}));

vi.mock('../tabs/HistoryTab', () => ({
  HistoryTab: () => <div>History tab</div>,
}));

describe('PortfolioView navigation', () => {
  it('navigates to the canonical plans route for exact plan drill-in and new exit plans', async () => {
    const user = userEvent.setup();
    render(
      <MemoryRouter>
        <PortfolioView />
      </MemoryRouter>,
    );

    await user.click(screen.getByRole('button', { name: 'open linked plan' }));
    expect(navigateMock).toHaveBeenCalledWith('/plans?setupId=setup-aapl');

    await user.click(screen.getByRole('button', { name: 'create exit plan' }));
    expect(navigateMock).toHaveBeenCalledWith('/plans?mode=sell&symbol=MSFT&tradeId=TRD-MSFT');
  });
});
