import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:get/get.dart';
import 'package:mocktail/mocktail.dart';
import 'package:rasadyar_inspection/presentation/widget/search.dart';
import 'package:rasadyar_inspection/presentation/widget/base_page/logic.dart';
import 'package:rasadyar_core/core.dart';

class MockBaseLogic extends GetxController with Mock implements BaseLogic {
  @override
  RxBool showSearch = false.obs;

  @override
  RxnString searchValue = RxnString();

  @override
  void setSearchCallback(void Function(String?)? callback) {}
}

void main() {
  group('SearchWidget Tests', () {
    late MockBaseLogic mockController;
    late void Function(String?)? mockCallback;

    setUp(() {
      mockController = MockBaseLogic();
      mockCallback = (String? value) {};

      Get.put<BaseLogic>(mockController);
    });

    tearDown(() {
      Get.reset();
    });

    testWidgets('should create SearchWidget with callback', (WidgetTester tester) async {
      // Act
      await tester.pumpWidget(
        MaterialApp(
          home: Scaffold(
            body: SearchWidget(onSearchChanged: mockCallback),
          ),
        ),
      );

      // Assert
      expect(find.byType(SearchWidget), findsOneWidget);
      verify(() => mockController.setSearchCallback(any())).called(1);
    });

    testWidgets('should show animated container when search is visible', (WidgetTester tester) async {
      // Arrange
      mockController.showSearch.value = true;

      // Act
      await tester.pumpWidget(
        MaterialApp(
          home: Scaffold(
            body: SearchWidget(onSearchChanged: mockCallback),
          ),
        ),
      );

      // Assert
      expect(find.byType(AnimatedContainer), findsOneWidget);
      expect(find.byType(Visibility), findsOneWidget);

      final visibility = tester.widget<Visibility>(find.byType(Visibility));
      expect(visibility.visible, true);
    });

    testWidgets('should hide search when showSearch is false', (WidgetTester tester) async {
      // Arrange
      mockController.showSearch.value = false;

      // Act
      await tester.pumpWidget(
        MaterialApp(
          home: Scaffold(
            body: SearchWidget(onSearchChanged: mockCallback),
          ),
        ),
      );
      await tester.pump();

      // Assert
      final animatedContainer = tester.widget<AnimatedContainer>(find.byType(AnimatedContainer));
      expect(animatedContainer.height, 0);
    });

    testWidgets('should display correct height when search is visible', (WidgetTester tester) async {
      // Arrange
      mockController.showSearch.value = true;

      // Act
      await tester.pumpWidget(
        MaterialApp(
          home: Scaffold(
            body: SearchWidget(onSearchChanged: mockCallback),
          ),
        ),
      );

      // Assert
      final animatedContainer = tester.widget<AnimatedContainer>(find.byType(AnimatedContainer));
      expect(animatedContainer.height, 40);
      expect(animatedContainer.duration, const Duration(milliseconds: 300));
      expect(animatedContainer.curve, Curves.easeInOut);
    });

    testWidgets('should have correct padding', (WidgetTester tester) async {
      // Arrange
      mockController.showSearch.value = true;

      // Act
      await tester.pumpWidget(
        MaterialApp(
          home: Scaffold(
            body: SearchWidget(onSearchChanged: mockCallback),
          ),
        ),
      );

      // Assert
      expect(find.byType(Padding), findsOneWidget);
      final padding = tester.widget<Padding>(find.byType(Padding));
      expect(padding.padding, const EdgeInsets.symmetric(horizontal: 8));
    });

    testWidgets('should work without callback', (WidgetTester tester) async {
      // Act
      await tester.pumpWidget(
        MaterialApp(
          home: Scaffold(
            body: SearchWidget(),
          ),
        ),
      );

      // Assert
      expect(find.byType(SearchWidget), findsOneWidget);
      verify(() => mockController.setSearchCallback(null)).called(1);
    });

    testWidgets('should have text editing controller', (WidgetTester tester) async {
      // Arrange
      mockController.showSearch.value = true;

      // Act
      await tester.pumpWidget(
        MaterialApp(
          home: Scaffold(
            body: SearchWidget(onSearchChanged: mockCallback),
          ),
        ),
      );

      // Assert - The widget should contain text editing functionality
      expect(find.byType(SearchWidget), findsOneWidget);
    });
  });
}
