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

import 'package:tasq/models/notification_item.dart';
import 'package:tasq/models/office.dart';
import 'package:tasq/models/profile.dart';
import 'package:tasq/models/task.dart';
import 'package:tasq/models/ticket.dart';
import 'package:tasq/models/user_office.dart';
import 'package:tasq/models/team.dart';
import 'package:tasq/models/team_member.dart';
import 'package:tasq/providers/notifications_provider.dart';
import 'package:tasq/providers/profile_provider.dart';
import 'package:tasq/providers/tasks_provider.dart';
import 'package:tasq/providers/tickets_provider.dart';
import 'package:tasq/providers/user_offices_provider.dart';
import 'package:tasq/screens/admin/offices_screen.dart';
import 'package:tasq/screens/admin/user_management_screen.dart';
import 'package:tasq/screens/tasks/tasks_list_screen.dart';
import 'package:tasq/screens/tickets/tickets_list_screen.dart';
import 'package:tasq/screens/tickets/ticket_detail_screen.dart';
import 'package:tasq/screens/teams/teams_screen.dart';
import 'package:tasq/providers/teams_provider.dart';
import 'package:tasq/widgets/app_shell.dart';

// (Noop typing controller removed — use provider overrides when needed.)

// Test double for NotificationsController so widget tests don't initialize
// a real Supabase client.
class FakeNotificationsController implements NotificationsController {
  @override
  Future<void> createMentionNotifications({
    required List<String> userIds,
    required String actorId,
    required int messageId,
    String? ticketId,
    String? taskId,
  }) async {}

  @override
  Future<void> markRead(String id) async {}

  @override
  Future<void> markReadForTicket(String ticketId) async {}

  @override
  Future<void> markReadForTask(String taskId) async {}
}

void main() {
  final now = DateTime(2026, 2, 10, 12, 0, 0);
  final office = Office(id: 'office-1', name: 'HQ');
  final admin = Profile(id: 'user-1', role: 'admin', fullName: 'Alex Admin');
  final tech = Profile(id: 'user-2', role: 'it_staff', fullName: 'Jamie Tech');
  final ticket = Ticket(
    id: 'TCK-1',
    subject: 'Printer down',
    description: 'Paper jam and offline',
    officeId: 'office-1',
    status: 'open',
    createdAt: now,
    creatorId: 'user-1',
    respondedAt: null,
    promotedAt: null,
    closedAt: null,
  );
  final task = Task(
    id: 'TSK-1',
    ticketId: 'TCK-1',
    taskNumber: '2026-02-00001',
    title: 'Reboot printer',
    description: 'Clear queue and reboot',
    officeId: 'office-1',
    status: 'queued',
    priority: 1,
    queueOrder: 1,
    createdAt: now,
    creatorId: 'user-2',
    startedAt: null,
    completedAt: null,
    requestType: null,
    requestTypeOther: null,
    requestCategory: null,
  );
  final notification = NotificationItem(
    id: 'N-1',
    userId: 'user-1',
    actorId: 'user-2',
    ticketId: 'TCK-1',
    taskId: null,
    messageId: 1,
    type: 'mention',
    createdAt: now,
    readAt: null,
  );

  List<Override> baseOverrides() {
    return [
      currentProfileProvider.overrideWith((ref) => Stream.value(admin)),
      profilesProvider.overrideWith((ref) => Stream.value([admin, tech])),

      officesProvider.overrideWith((ref) => Stream.value([office])),
      notificationsProvider.overrideWith((ref) => Stream.value([notification])),
      ticketsProvider.overrideWith((ref) => Stream.value([ticket])),
      tasksProvider.overrideWith((ref) => Stream.value([task])),
      tasksControllerProvider.overrideWith((ref) => TasksController(null)),
      userOfficesProvider.overrideWith(
        (ref) =>
            Stream.value([UserOffice(userId: 'user-1', officeId: 'office-1')]),
      ),
      ticketMessagesAllProvider.overrideWith((ref) => const Stream.empty()),
      ticketMessagesProvider.overrideWith((ref, id) => const Stream.empty()),
      isAdminProvider.overrideWith((ref) => true),
      notificationsControllerProvider.overrideWithValue(
        FakeNotificationsController(),
      ),
    ];
  }

  List<Override> userManagementOverrides() {
    return [
      currentProfileProvider.overrideWith((ref) => Stream.value(admin)),
      profilesProvider.overrideWith((ref) => Stream.value(const <Profile>[])),
      officesProvider.overrideWith((ref) => Stream.value([office])),
      userOfficesProvider.overrideWith(
        (ref) => Stream.value(const <UserOffice>[]),
      ),
      ticketMessagesAllProvider.overrideWith((ref) => const Stream.empty()),
      ticketMessagesProvider.overrideWith((ref, id) => const Stream.empty()),
      isAdminProvider.overrideWith((ref) => true),
    ];
  }

  group('Layout smoke tests', () {
    testWidgets('Tickets list renders without layout exceptions', (
      tester,
    ) async {
      await _setSurfaceSize(tester, const Size(1024, 800));
      await _pumpScreen(
        tester,
        const TicketsListScreen(),
        overrides: baseOverrides(),
      );
      await tester.pump();
      await tester.pump(const Duration(milliseconds: 16));
      expect(tester.takeException(), isNull);
    });

    testWidgets('Tasks list renders without layout exceptions', (tester) async {
      await _setSurfaceSize(tester, const Size(1024, 800));
      await _pumpScreen(
        tester,
        const TasksListScreen(),
        overrides: baseOverrides(),
      );
      await tester.pump();
      await tester.pump(const Duration(milliseconds: 16));
      expect(tester.takeException(), isNull);
    });

    testWidgets('Offices screen renders without layout exceptions', (
      tester,
    ) async {
      await _setSurfaceSize(tester, const Size(1024, 800));
      await _pumpScreen(
        tester,
        const OfficesScreen(),
        overrides: baseOverrides(),
      );
      await tester.pump();
      await tester.pump(const Duration(milliseconds: 16));
      expect(tester.takeException(), isNull);
    });

    testWidgets('Teams screen renders without layout exceptions', (
      tester,
    ) async {
      await _setSurfaceSize(tester, const Size(1024, 800));
      await _pumpScreen(
        tester,
        const TeamsScreen(),
        overrides: [
          ...baseOverrides(),
          teamsProvider.overrideWith((ref) => Stream.value(const <Team>[])),
          teamMembersProvider.overrideWith(
            (ref) => Stream.value(const <TeamMember>[]),
          ),
        ],
      );
      await tester.pump();
      await tester.pump(const Duration(milliseconds: 16));
      expect(tester.takeException(), isNull);
    });

    testWidgets('App shell shows Geofence test nav item for admin', (
      tester,
    ) async {
      await _setSurfaceSize(tester, const Size(1024, 800));
      await _pumpScreen(
        tester,
        const AppScaffold(child: SizedBox()),
        overrides: baseOverrides(),
      );
      await tester.pumpAndSettle();
      expect(find.text('Geofence test'), findsOneWidget);
    });

    testWidgets('Add Team dialog requires at least one office', (tester) async {
      await _setSurfaceSize(tester, const Size(600, 800));
      await _pumpScreen(
        tester,
        const TeamsScreen(),
        overrides: [
          ...baseOverrides(),
          teamsProvider.overrideWith((ref) => Stream.value(const <Team>[])),
          teamMembersProvider.overrideWith(
            (ref) => Stream.value(const <TeamMember>[]),
          ),
        ],
      );

      // Open Add Team dialog
      await tester.tap(find.byType(FloatingActionButton));
      await tester.pumpAndSettle();

      // Enter name
      final nameField = find.byType(TextField).first;
      await tester.enterText(nameField, 'Alpha Team');
      await tester.pumpAndSettle();

      // Select leader (it_staff)
      final leaderDropdown = find.widgetWithText(
        DropdownButtonFormField<String>,
        'Team Leader',
      );
      await tester.tap(leaderDropdown);
      await tester.pumpAndSettle();
      await tester.tap(find.text('Jamie Tech').last);
      await tester.pumpAndSettle();

      // Try to add without selecting offices -> should show validation SnackBar
      await tester.tap(find.text('Add'));
      await tester.pumpAndSettle();

      expect(
        find.text('Assign at least one office to the team'),
        findsOneWidget,
      );
    });

    testWidgets('Add Team dialog: opening Offices dropdown does not overflow', (
      tester,
    ) async {
      await _setSurfaceSize(tester, const Size(600, 800));
      await _pumpScreen(
        tester,
        const TeamsScreen(),
        overrides: [
          ...baseOverrides(),
          teamsProvider.overrideWith((ref) => Stream.value(const <Team>[])),
          teamMembersProvider.overrideWith(
            (ref) => Stream.value(const <TeamMember>[]),
          ),
        ],
      );

      // Open Add Team dialog
      await tester.tap(find.byType(FloatingActionButton));
      await tester.pumpAndSettle();

      // Find Offices InputDecorator and tap its Select ActionChip
      final officesField = find.widgetWithText(InputDecorator, 'Offices');
      expect(officesField, findsOneWidget);
      final selectChip = find.descendant(
        of: officesField,
        matching: find.widgetWithText(ActionChip, 'Select'),
      );
      expect(selectChip, findsOneWidget);

      await tester.tap(selectChip);
      await tester.pumpAndSettle();

      // Inner dialog should appear and no layout overflow exceptions should be thrown
      expect(find.text('Select Offices'), findsOneWidget);
      expect(tester.takeException(), isNull);

      // The list should contain the sample office
      expect(find.widgetWithText(CheckboxListTile, 'HQ'), findsOneWidget);
    });

    testWidgets('User management renders without layout exceptions', (
      tester,
    ) async {
      await _setSurfaceSize(tester, const Size(1024, 800));
      await _pumpScreen(
        tester,
        const UserManagementScreen(),
        overrides: userManagementOverrides(),
      );
      await tester.pump();
      await tester.pump(const Duration(milliseconds: 16));
      expect(tester.takeException(), isNull);
    });

    testWidgets('Typing indicator: no post-dispose state mutation', (
      tester,
    ) async {
      await _setSurfaceSize(tester, const Size(600, 800));

      // Show TicketDetailScreen with the base overrides (includes typing controller).
      await _pumpScreen(
        tester,
        const TicketDetailScreen(ticketId: 'TCK-1'),
        overrides: baseOverrides(),
      );

      await tester.pump();

      // Find message TextField and simulate typing.
      final finder = find.byType(TextField);
      expect(finder, findsWidgets);
      await tester.enterText(finder.first, 'Hello');

      // Immediately remove the screen (navigate away / dispose).
      await tester.pumpWidget(Container());

      // Let pending timers (typing stop, remote timeouts) run.
      await tester.pump(const Duration(milliseconds: 500));
      await tester.pumpAndSettle();

      // No unhandled exceptions should have been thrown.
      expect(tester.takeException(), isNull);
    });
  });
}

Future<void> _pumpScreen(
  WidgetTester tester,
  Widget child, {
  required List<Override> overrides,
}) async {
  await tester.pumpWidget(
    ProviderScope(
      overrides: overrides,
      child: MaterialApp(home: Scaffold(body: child)),
    ),
  );
}

Future<void> _setSurfaceSize(WidgetTester tester, Size size) async {
  await tester.binding.setSurfaceSize(size);
  addTearDown(() async {
    await tester.binding.setSurfaceSize(null);
  });
}
