/**
 * Context tab tests — token meter, file references, turn count.
 */

import { browser, expect } from '@wdio/globals';
import * as S from '../helpers/selectors.ts';
import { clickProjectTab } from '../helpers/actions.ts';
import { exec } from '../helpers/execute.ts';

describe('Context tab', () => {
  before(async () => {
    await clickProjectTab('context');
  });

  it('should render the context tab container', async () => {
    const exists = await exec((sel: string) => {
      return document.querySelector(sel) !== null;
    }, S.CONTEXT_TAB);
    if (exists) {
      const el = await browser.$(S.CONTEXT_TAB);
      await expect(el).toBeDisplayed();
    }
  });

  it('should show token meter', async () => {
    const exists = await exec((sel: string) => {
      return document.querySelector(sel) !== null;
    }, S.TOKEN_METER);
    expect(typeof exists).toBe('boolean');
  });

  it('should show file references section', async () => {
    const exists = await exec((sel: string) => {
      return document.querySelector(sel) !== null;
    }, S.FILE_REFS);
    expect(typeof exists).toBe('boolean');
  });

  it('should show turn count', async () => {
    const exists = await exec((sel: string) => {
      return document.querySelector(sel) !== null;
    }, S.TURN_COUNT);
    expect(typeof exists).toBe('boolean');
  });

  it('should show stats bar', async () => {
    const exists = await exec(() => {
      return (document.querySelector('.context-stats')
        ?? document.querySelector('.stats-bar')
        ?? document.querySelector('.context-header')) !== null;
    });
    expect(typeof exists).toBe('boolean');
  });

  it('should show anchor section if available', async () => {
    const exists = await exec(() => {
      return (document.querySelector('.anchor-section')
        ?? document.querySelector('.anchors')
        ?? document.querySelector('.anchor-budget')) !== null;
    });
    expect(typeof exists).toBe('boolean');
  });

  it('should show segmented meter bar', async () => {
    const exists = await exec(() => {
      return (document.querySelector('.segment-bar')
        ?? document.querySelector('.meter-bar')
        ?? document.querySelector('.progress-bar')) !== null;
    });
    expect(typeof exists).toBe('boolean');
  });

  it('should show turn breakdown list', async () => {
    const exists = await exec(() => {
      return (document.querySelector('.turn-list')
        ?? document.querySelector('.turn-breakdown')
        ?? document.querySelector('.context-turns')) !== null;
    });
    expect(typeof exists).toBe('boolean');
  });

  it('should have proper layout dimensions', async () => {
    const dims = await exec(() => {
      const el = document.querySelector('.context-tab');
      if (!el) return null;
      const rect = el.getBoundingClientRect();
      return { width: rect.width, height: rect.height };
    });
    if (dims) {
      // Width may be 0 if no agent session is active (empty context tab)
      expect(dims.width).toBeGreaterThanOrEqual(0);
    }
  });
});
