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

import 'package:chanora_flutter/src/rust/api.dart' as rust;
import 'package:chanora_flutter/widgets/chat_panel.dart';
import 'package:chanora_flutter/widgets/chat_views.dart';

void main() {
  rust.BridgeSnapshot snapshot() {
    final channelId = BigInt.from(10);
    return rust.BridgeSnapshot(
      serverName: 'Server',
      welcomeMessage: '',
      platform: '',
      version: '',
      channels: [
        rust.BridgeChannel(
          id: channelId,
          parent: BigInt.zero,
          name: 'Lobby',
          order: 0,
          hasPassword: false,
          neededTalkPower: 0,
        ),
      ],
      clients: [
        rust.BridgeClient(
          id: BigInt.one,
          channel: channelId,
          name: 'Me',
          inputMuted: false,
          outputMuted: false,
          isSpeaking: false,
          isServerQuery: false,
          talkPower: 0,
          talkPowerGranted: true,
        ),
      ],
      ownClientId: BigInt.one,
    );
  }

  testWidgets('inline chat panel renders target, messages, and close action', (
    tester,
  ) async {
    var closed = false;
    final messages = [
      ChatEntry(
        senderId: BigInt.from(2),
        senderName: 'Alice',
        message: 'Hello from channel',
        target: const rust.BridgeMessageTarget.channel(),
      ),
    ];

    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          body: ChatPanel(
            messages: messages,
            snapshot: snapshot(),
            target: const rust.BridgeMessageTarget.channel(),
            clientName: '',
            onClose: () => closed = true,
          ),
        ),
      ),
    );

    expect(find.text('# Lobby'), findsOneWidget);
    expect(find.text('Hello from channel'), findsOneWidget);

    await tester.tap(find.byTooltip('Close chat'));
    await tester.pump();

    expect(closed, isTrue);
  });

  testWidgets('chat detail restores target drafts when the target changes', (
    tester,
  ) async {
    String? savedDraft;
    final messages = <ChatEntry>[];

    Widget detail({
      required rust.BridgeMessageTarget target,
      required String? restoredDraft,
    }) {
      return MaterialApp(
        home: Scaffold(
          body: ChatDetailView(
            messages: messages,
            snapshot: snapshot(),
            target: target,
            clientName: '',
            currentChannelId: BigInt.from(10),
            channelName: 'Lobby',
            restoredDraft: restoredDraft,
            onDraftChanged: (text) => savedDraft = text,
          ),
        ),
      );
    }

    await tester.pumpWidget(
      detail(
        target: const rust.BridgeMessageTarget.channel(),
        restoredDraft: 'channel draft',
      ),
    );

    expect(
      tester.widget<TextField>(find.byType(TextField)).controller!.text,
      'channel draft',
    );

    await tester.enterText(find.byType(TextField), 'typed channel draft');
    await tester.pumpWidget(
      detail(
        target: const rust.BridgeMessageTarget.server(),
        restoredDraft: 'server draft',
      ),
    );

    expect(savedDraft, 'typed channel draft');
    expect(
      tester.widget<TextField>(find.byType(TextField)).controller!.text,
      'server draft',
    );
  });
}
