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

class MockAuthLogic extends GetxController with Mock implements AuthLogic {
  @override
  late Animation<double> textAnimation;

  @override
  RxBool isLoading = false.obs;

  @override
  RxBool obscurePassword = true.obs;

  @override
  TextEditingController usernameController = TextEditingController();

  @override
  TextEditingController passwordController = TextEditingController();
}

class MockAnimationController extends Mock implements AnimationController {}

void main() {
  group('AuthPage Tests', () {
    late MockAuthLogic mockController;
    late MockAnimationController mockAnimationController;

    setUp(() {
      mockController = MockAuthLogic();
      mockAnimationController = MockAnimationController();

      // Setup mock animation
      mockController.textAnimation = AlwaysStoppedAnimation(1.0);

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

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

    testWidgets('should display welcome text in Persian', (WidgetTester tester) async {
      // Act
      await tester.pumpWidget(
        MaterialApp(
          home: AuthPage(),
        ),
      );

      // Assert
      expect(find.text('به سامانه رصدیار خوش آمدید!'), findsOneWidget);
      expect(find.text('سامانه رصد و پایش زنجیره تامین، تولید و توزیع کالا های اساسی'), findsOneWidget);
    });

    testWidgets('should have correct scaffold structure', (WidgetTester tester) async {
      // Act
      await tester.pumpWidget(
        MaterialApp(
          home: AuthPage(),
        ),
      );

      // Assert
      expect(find.byType(Scaffold), findsOneWidget);
      expect(find.byType(PopScope), findsOneWidget);
      expect(find.byType(Stack), findsOneWidget);
      expect(find.byType(FadeTransition), findsOneWidget);
    });

    testWidgets('should display welcome texts with correct styling', (WidgetTester tester) async {
      // Act
      await tester.pumpWidget(
        MaterialApp(
          home: AuthPage(),
        ),
      );

      // Assert
      final welcomeText = tester.widget<Text>(
        find.text('به سامانه رصدیار خوش آمدید!')
      );
      expect(welcomeText.style?.color, Colors.white);

      final descriptionText = tester.widget<Text>(
        find.text('سامانه رصد و پایش زنجیره تامین، تولید و توزیع کالا های اساسی')
      );
      expect(descriptionText.style?.color, Colors.white);
      expect(descriptionText.textAlign, TextAlign.center);
    });

    testWidgets('should have background SVG asset', (WidgetTester tester) async {
      // Act
      await tester.pumpWidget(
        MaterialApp(
          home: AuthPage(),
        ),
      );

      // Assert
      expect(find.byType(Stack), findsOneWidget);
      // The SVG background should be present in the stack
      final stack = tester.widget<Stack>(find.byType(Stack));
      expect(stack.fit, StackFit.expand);
      expect(stack.alignment, Alignment.center);
    });

    testWidgets('should handle PopScope correctly', (WidgetTester tester) async {
      // Act
      await tester.pumpWidget(
        MaterialApp(
          home: AuthPage(),
        ),
      );

      // Assert
      final popScope = tester.widget<PopScope>(find.byType(PopScope));
      expect(popScope.canPop, false);
    });

    testWidgets('should display column with correct spacing', (WidgetTester tester) async {
      // Act
      await tester.pumpWidget(
        MaterialApp(
          home: AuthPage(),
        ),
      );

      // Assert
      expect(find.byType(Column), findsOneWidget);
      final column = tester.widget<Column>(find.byType(Column));
      expect(column.mainAxisSize, MainAxisSize.min);
      expect(column.mainAxisAlignment, MainAxisAlignment.start);
      expect(column.crossAxisAlignment, CrossAxisAlignment.center);
    });
  });
}
