import { render, screen } from '@testing-library/react';
import { beforeEach, describe, expect, test, vi } from 'vitest';

import DashboardRoute from '../../src/app/(app)/dashboard';
import SettingsRoute from '../../src/app/(app)/settings';
import SpoonsRoute from '../../src/app/(app)/spoons';
import ThreadsRoute from '../../src/app/(app)/threads';
import WorkspaceRoute from '../../src/app/(app)/workspace/[jobId]';
import { mockedUseQuery } from '../setup';

describe('mobile route smoke tests', () => {
  beforeEach(() => {
    vi.clearAllMocks();
    mockedUseQuery.mockReset();
  });

  test('Dashboard renders metrics from mocked Convex data', () => {
    mockedUseQuery
      .mockReturnValueOnce([
        {
          _id: 'spoon-1',
          status: 'active',
          syncStatus: 'behind',
          upstreamAheadBy: 3,
        },
      ] as never)
      .mockReturnValueOnce([] as never)
      .mockReturnValueOnce([
        {
          _id: 'thread-1',
          source: 'user_request',
          status: 'open',
          title: 'Update auth',
          updatedAt: Date.UTC(2026, 0, 1),
        },
      ] as never);

    render(<DashboardRoute />);

    expect(screen.getByText('Dashboard')).toBeTruthy();
    expect(screen.getByText('Update auth')).toBeTruthy();
    expect(screen.getByText('Upstream commits')).toBeTruthy();
  });

  test('Spoons list renders empty state and one row', () => {
    mockedUseQuery
      .mockReturnValueOnce([
        {
          _id: 'spoon-1',
          forkOwner: 'gib',
          forkRepo: 'usesend',
          name: 'usesend-authentik',
          status: 'active',
          syncStatus: 'up_to_date',
          upstreamAheadBy: 0,
          upstreamOwner: 'usesend',
          upstreamRepo: 'usesend',
        },
      ] as never)
      .mockReturnValueOnce([] as never);

    render(<SpoonsRoute />);

    expect(screen.getByText('Spoons')).toBeTruthy();
    expect(screen.getByText('usesend-authentik')).toBeTruthy();
  });

  test('Threads list renders filters and rows', () => {
    mockedUseQuery.mockReturnValueOnce([
      {
        _id: 'thread-1',
        source: 'upstream_update',
        status: 'waiting_for_user',
        title: 'Upstream auth changes landed',
        updatedAt: Date.UTC(2026, 0, 1),
      },
    ] as never);

    render(<ThreadsRoute />);

    expect(screen.getByText('Waiting')).toBeTruthy();
    expect(screen.getByText('Upstream auth changes landed')).toBeTruthy();
  });

  test('Workspace route renders tabs and job status', () => {
    mockedUseQuery
      .mockReturnValueOnce({
        _id: 'job-1',
        model: 'gpt-5.1-codex',
        reasoningEffort: 'medium',
        status: 'running',
        workBranch: 'spoon/thread/example',
        workspaceStatus: 'active',
      } as never)
      .mockReturnValueOnce([] as never)
      .mockReturnValueOnce([] as never)
      .mockReturnValueOnce([] as never);

    render(<WorkspaceRoute />);

    expect(screen.getByText('Workspace review')).toBeTruthy();
    expect(screen.getByText('Messages')).toBeTruthy();
    expect(screen.getByText('running')).toBeTruthy();
  });

  test('Settings index renders GitHub and AI provider summaries', () => {
    mockedUseQuery
      .mockReturnValueOnce({ email: 'gib@example.com' } as never)
      .mockReturnValueOnce({
        displayName: 'gibbyb',
        status: 'active',
      } as never)
      .mockReturnValueOnce([
        {
          _id: 'provider-1',
          isDefault: true,
          name: 'Codex',
        },
      ] as never);

    render(<SettingsRoute />);

    expect(screen.getByText('gib@example.com')).toBeTruthy();
    expect(screen.getByText('GitHub connected as gibbyb')).toBeTruthy();
    expect(screen.getByText('1 provider, default Codex')).toBeTruthy();
  });
});
