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

import 'package:chanora_flutter/widgets/app_snack_bar.dart';

void main() {
  test('app snackbar theme uses light Material 3 surface styling', () {
    final scheme = ColorScheme.fromSeed(seedColor: const Color(0xFF3F51B5));
    final theme = AppSnackBar.theme(scheme);

    expect(theme.behavior, SnackBarBehavior.floating);
    expect(theme.backgroundColor, scheme.surfaceContainerHigh);
    expect(theme.contentTextStyle?.color, scheme.onSurface);
    expect(theme.actionTextColor, scheme.primary);
    expect(theme.shape, isA<RoundedRectangleBorder>());
  });

  testWidgets('app snackbar helper applies bounded light error variant', (
    tester,
  ) async {
    tester.view.physicalSize = const Size(1000, 800);
    tester.view.devicePixelRatio = 1.0;
    addTearDown(tester.view.resetPhysicalSize);
    addTearDown(tester.view.resetDevicePixelRatio);

    const seed = Color(0xFF3F51B5);
    final scheme = ColorScheme.fromSeed(seedColor: seed);

    await tester.pumpWidget(
      MaterialApp(
        theme: ThemeData(
          useMaterial3: true,
          colorSchemeSeed: seed,
          snackBarTheme: AppSnackBar.theme(scheme),
        ),
        home: Scaffold(
          body: Builder(
            builder: (context) => TextButton(
              onPressed: () {
                ScaffoldMessenger.of(context).showSnackBar(
                  AppSnackBar.text(
                    context: context,
                    message: 'Could not connect',
                    variant: AppSnackBarVariant.error,
                    maxLines: 3,
                    overflow: TextOverflow.ellipsis,
                  ),
                );
              },
              child: const Text('Show'),
            ),
          ),
        ),
      ),
    );

    await tester.tap(find.text('Show'));
    await tester.pump();

    final snackBar = tester.widget<SnackBar>(find.byType(SnackBar));
    expect(snackBar.behavior, SnackBarBehavior.floating);
    expect(snackBar.width, 560);
    expect(snackBar.backgroundColor, scheme.errorContainer);

    final text = tester.widget<Text>(find.text('Could not connect'));
    expect(text.maxLines, 3);
    expect(text.overflow, TextOverflow.ellipsis);
  });

  testWidgets('app snackbar desktop width cap survives custom margin', (
    tester,
  ) async {
    tester.view.physicalSize = const Size(1000, 800);
    tester.view.devicePixelRatio = 1.0;
    addTearDown(tester.view.resetPhysicalSize);
    addTearDown(tester.view.resetDevicePixelRatio);

    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          body: Builder(
            builder: (context) => TextButton(
              onPressed: () {
                ScaffoldMessenger.of(context).showSnackBar(
                  AppSnackBar.text(
                    context: context,
                    message: 'Private message from observer',
                    margin: const EdgeInsets.all(24),
                  ),
                );
              },
              child: const Text('Show'),
            ),
          ),
        ),
      ),
    );

    await tester.tap(find.text('Show'));
    await tester.pump();

    final snackBar = tester.widget<SnackBar>(find.byType(SnackBar));
    expect(snackBar.width, isNull);
    expect(snackBar.margin, const EdgeInsets.fromLTRB(244, 24, 244, 24));
  });
}
