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/captcha/logic.dart';
import 'package:rasadyar_inspection/presentation/widget/captcha/view.dart';
import 'package:rasadyar_core/core.dart';

class MockCaptchaWidgetLogic extends GetxController
    with StateMixin<CaptchaModel>, Mock implements CaptchaWidgetLogic {
  @override
  GlobalKey<FormState> formKey = GlobalKey<FormState>();

  @override
  TextEditingController captchaController = TextEditingController();

  @override
  void getCaptcha() {}
}

class CaptchaModel {
  final String captchaImage;
  final String captchaToken;

  CaptchaModel({required this.captchaImage, required this.captchaToken});
}

void main() {
  group('CaptchaWidget Tests', () {
    late MockCaptchaWidgetLogic mockController;

    setUp(() {
      mockController = MockCaptchaWidgetLogic();
      Get.put<CaptchaWidgetLogic>(mockController);
    });

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

    testWidgets('should display captcha container with correct styling', (WidgetTester tester) async {
      // Arrange
      when(() => mockController.obx(any(), onLoading: any(named: 'onLoading'), onError: any(named: 'onError')))
          .thenReturn(Container());

      // Act
      await tester.pumpWidget(
        MaterialApp(
          home: Scaffold(
            body: CaptchaWidget(),
          ),
        ),
      );

      // Assert
      expect(find.byType(Row), findsOneWidget);
      expect(find.byType(GestureDetector), findsOneWidget);
      expect(find.byType(Container), findsWidgets);

      final container = tester.widget<Container>(find.byType(Container).first);
      expect(container.clipBehavior, Clip.antiAliasWithSaveLayer);
    });

    testWidgets('should have correct form structure', (WidgetTester tester) async {
      // Arrange
      when(() => mockController.obx(any(), onLoading: any(named: 'onLoading'), onError: any(named: 'onError')))
          .thenReturn(Container());

      // Act
      await tester.pumpWidget(
        MaterialApp(
          home: Scaffold(
            body: CaptchaWidget(),
          ),
        ),
      );

      // Assert
      expect(find.byType(Form), findsOneWidget);
      expect(find.byType(Expanded), findsOneWidget);

      final form = tester.widget<Form>(find.byType(Form));
      expect(form.autovalidateMode, AutovalidateMode.disabled);
    });

    testWidgets('should handle tap on captcha container', (WidgetTester tester) async {
      // Arrange
      when(() => mockController.obx(any(), onLoading: any(named: 'onLoading'), onError: any(named: 'onError')))
          .thenReturn(Container());

      // Act
      await tester.pumpWidget(
        MaterialApp(
          home: Scaffold(
            body: CaptchaWidget(),
          ),
        ),
      );

      // Find and tap the gesture detector
      await tester.tap(find.byType(GestureDetector));
      await tester.pump();

      // Assert
      verify(() => mockController.getCaptcha()).called(1);
    });

    testWidgets('should display correct row layout', (WidgetTester tester) async {
      // Arrange
      when(() => mockController.obx(any(), onLoading: any(named: 'onLoading'), onError: any(named: 'onError')))
          .thenReturn(Container());

      // Act
      await tester.pumpWidget(
        MaterialApp(
          home: Scaffold(
            body: CaptchaWidget(),
          ),
        ),
      );

      // Assert
      final row = tester.widget<Row>(find.byType(Row));
      expect(row.crossAxisAlignment, CrossAxisAlignment.start);
    });

    testWidgets('should have SizedBox with correct width', (WidgetTester tester) async {
      // Arrange
      when(() => mockController.obx(any(), onLoading: any(named: 'onLoading'), onError: any(named: 'onError')))
          .thenReturn(Container());

      // Act
      await tester.pumpWidget(
        MaterialApp(
          home: Scaffold(
            body: CaptchaWidget(),
          ),
        ),
      );

      // Assert
      expect(find.byType(SizedBox), findsOneWidget);
      final sizedBox = tester.widget<SizedBox>(find.byType(SizedBox));
      expect(sizedBox.width, 8);
    });
  });
}
