import 'package:flutter_test/flutter_test.dart';

import 'package:feature_orders/feature_orders.dart';
import 'package:feature_orders/src/application/get_orders.dart';
import 'package:feature_orders/src/application/orders_controller.dart';

void main() {
  late FakeOrdersRepository repository;
  late OrdersController controller;

  setUp(() {
    repository = FakeOrdersRepository();
    controller = OrdersController(GetOrders(repository));
  });

  tearDown(() {
    controller.dispose();
  });

  group('OrdersController', () {
    test('starts with empty state', () {
      expect(controller.isLoading, false);
      expect(controller.orders, isEmpty);
      expect(controller.selectedOrder, isNull);
      expect(controller.activeFilter, isNull);
      expect(controller.searchQuery, '');
      expect(controller.error, isNull);
    });

    test('load populates orders and auto-selects first', () async {
      await controller.load();

      expect(controller.isLoading, false);
      expect(controller.orders.length, 6);
      expect(controller.selectedOrder, isNotNull);
      expect(controller.selectedOrder!.id, 'KC-1001');
      expect(controller.error, isNull);
    });

    test('selectOrder updates selectedOrder', () async {
      await controller.load();

      final third = controller.orders[2];
      controller.selectOrder(third);

      expect(controller.selectedOrder!.id, third.id);
    });

    test('setFilter filters by order status', () async {
      await controller.load();

      controller.setFilter('pending');
      expect(controller.orders.length, 2);
      expect(controller.orders.every((o) => o.status == OrderStatus.pending), true);
    });

    test('setFilter with null shows all orders', () async {
      await controller.load();

      controller.setFilter('cancelled');
      expect(controller.orders.length, 1);

      controller.setFilter(null);
      expect(controller.orders.length, 6);
    });

    test('setSearchQuery filters by customer name', () async {
      await controller.load();

      controller.setSearchQuery('sarah');
      expect(controller.orders.length, 1);
      expect(controller.orders.first.customerName, 'Sarah Mitchell');
    });

    test('setSearchQuery filters by order ID', () async {
      await controller.load();

      controller.setSearchQuery('KC-1003');
      expect(controller.orders.length, 1);
      expect(controller.orders.first.id, 'KC-1003');
    });

    test('filter and search combine', () async {
      await controller.load();

      controller.setFilter('pending');
      controller.setSearchQuery('david');
      expect(controller.orders.length, 1);
      expect(controller.orders.first.customerName, 'David Park');
    });

    test('selectById selects matching order', () async {
      await controller.load();

      final found = controller.selectById('KC-1004');
      expect(found, true);
      expect(controller.selectedOrder!.id, 'KC-1004');
    });

    test('selectById returns false for unknown ID', () async {
      await controller.load();

      final found = controller.selectById('UNKNOWN');
      expect(found, false);
    });

    test('selection is cleared when filtered out', () async {
      await controller.load();

      // Select a delivered order.
      controller.selectById('KC-1001');
      expect(controller.selectedOrder, isNotNull);

      // Filter to pending — the selected order should be cleared.
      controller.setFilter('pending');
      expect(controller.selectedOrder, isNull);
    });
  });
}
