import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mam_core/mam_core.dart';

import 'package:mam_desktop/src/providers/session_providers.dart';
import 'package:mam_desktop/src/theme/app_theme.dart';
import 'package:mam_desktop/src/widgets/detail_pane.dart';
import 'package:mam_desktop/src/widgets/session_table.dart';

void main() {
  testWidgets('SessionTable renders rows and drives selection', (tester) async {
    final rows = [
      SessionRow(
        name: 'demo-session',
        server: 'default',
        status: 'running',
        tmuxAlive: true,
        pane: const Pane(
          pid: 123,
          cwd: '/x',
          cmd: 'claude',
          cmdFull: 'claude --foo',
        ),
        role: 'creator',
        resumeState: 'yes',
        jobId: '-',
        jobStatus: '-',
        attachCommand: 'tmux attach -t demo-session',
        driftClasses: const ['B'],
      ),
    ];

    String? selected;
    await tester.pumpWidget(
      MaterialApp(
        theme: buildAppTheme(),
        home: Scaffold(
          body: SessionTable(
            sessions: rows,
            selectedName: null,
            onSelect: (name) => selected = name,
          ),
        ),
      ),
    );

    expect(find.text('demo-session'), findsOneWidget);
    expect(find.text('running'), findsOneWidget);

    await tester.tap(find.text('demo-session'));
    await tester.pump();
    expect(selected, 'demo-session');
  });

  testWidgets('DetailPane shows placeholder when nothing selected', (
    tester,
  ) async {
    await tester.pumpWidget(
      MaterialApp(
        theme: buildAppTheme(),
        home: const Scaffold(body: DetailPane(session: null)),
      ),
    );

    expect(find.text('Select a session to see details'), findsOneWidget);
  });

  test('selectedSessionNameProvider defaults to null', () {
    final container = ProviderContainer();
    addTearDown(container.dispose);
    expect(container.read(selectedSessionNameProvider), isNull);
  });
}
