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

import 'package:chanora_flutter/l10n/generated/app_localizations.dart';
import 'package:chanora_flutter/services/channel_join_error_mapper.dart';
import 'package:chanora_flutter/src/rust/lib.dart' as rust_err;

void main() {
  Future<AppL10n> loadEnglishL10n(WidgetTester tester) async {
    late AppL10n l10n;
    await tester.pumpWidget(
      MaterialApp(
        localizationsDelegates: AppL10n.localizationsDelegates,
        supportedLocales: AppL10n.supportedLocales,
        home: Builder(
          builder: (context) {
            l10n = AppL10n.of(context);
            return const SizedBox.shrink();
          },
        ),
      ),
    );
    return l10n;
  }

  testWidgets('maps known TS3 server rejection codes', (tester) async {
    final l10n = await loadEnglishL10n(tester);

    expect(
      channelJoinErrorMessage(
        l10n,
        const rust_err.BridgeError.serverRejected(
          code: 0x030d,
          message: 'invalid password',
        ),
      ),
      l10n.channelJoinFailedPassword,
    );
    expect(
      channelJoinErrorMessage(
        l10n,
        const rust_err.BridgeError.serverRejected(
          code: 0x0a08,
          message: 'insufficient permissions',
        ),
      ),
      l10n.channelJoinFailedPermission,
    );
  });

  testWidgets('falls back for generic and unknown errors', (tester) async {
    final l10n = await loadEnglishL10n(tester);

    expect(
      channelJoinErrorMessage(
        l10n,
        const rust_err.BridgeError.serverRejected(
          code: 0xffff,
          message: 'custom server error',
        ),
      ),
      l10n.channelJoinFailedGeneric('custom server error'),
    );
    expect(
      channelJoinErrorMessage(l10n, 'plain error'),
      l10n.channelJoinFailedGeneric('plain error'),
    );
  });
}
