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

import 'package:superport_v2/core/common/models/paginated_result.dart';
import 'package:superport_v2/core/navigation/menu_catalog.dart';
import 'package:superport_v2/core/navigation/route_paths.dart';
import 'package:superport_v2/core/permissions/permission_manager.dart';
import 'package:superport_v2/core/theme/superport_shad_theme.dart';
import 'package:superport_v2/features/masters/menu/domain/entities/menu.dart';
import 'package:superport_v2/features/masters/menu/domain/repositories/menu_repository.dart';

Widget buildTestApp(Widget child, {PermissionManager? permissionManager}) {
  final catalog = createTestMenuCatalog();
  return PermissionScope(
    manager: permissionManager ?? PermissionManager(),
    child: MenuCatalogScope(
      catalog: catalog,
      child: ShadApp(
        debugShowCheckedModeBanner: false,
        theme: SuperportShadTheme.light(),
        darkTheme: SuperportShadTheme.dark(),
        home: ScaffoldMessenger(child: Scaffold(body: child)),
      ),
    ),
  );
}

MenuCatalog createTestMenuCatalog({
  List<MenuItem>? menus,
  MenuRepository? repository,
}) {
  final catalog = MenuCatalog(
    repository: repository ?? _TestMenuRepository(),
  );
  final seedMenus = menus ?? _defaultMenuItems;
  catalog.replaceAll(List<MenuItem>.from(seedMenus));
  addTearDown(catalog.dispose);
  return catalog;
}

final List<MenuItem> _defaultMenuItems = List<MenuItem>.unmodifiable([
  MenuItem(
    id: 1,
    menuCode: 'dashboard',
    menuName: '대시보드',
    path: dashboardRoutePath,
    displayOrder: 10,
  ),
  MenuItem(
    id: 2,
    menuCode: 'inventory.summary',
    menuName: '재고 현황',
    path: inventorySummaryRoutePath,
    displayOrder: 20,
  ),
  MenuItem(
    id: 3,
    menuCode: 'inventory.receipts',
    menuName: '입고',
    path: inventoryReceiptsRoutePath,
    displayOrder: 21,
  ),
  MenuItem(
    id: 4,
    menuCode: 'inventory.issues',
    menuName: '출고',
    path: inventoryIssuesRoutePath,
    displayOrder: 22,
  ),
  MenuItem(
    id: 5,
    menuCode: 'settings.users',
    menuName: '사용자 관리',
    path: settingsUsersRoutePath,
    displayOrder: 40,
  ),
  MenuItem(
    id: 6,
    menuCode: 'settings.group_permissions',
    menuName: '그룹 메뉴 권한',
    path: settingsGroupPermissionsRoutePath,
    displayOrder: 43,
  ),
]);

class _TestMenuRepository implements MenuRepository {
  @override
  Future<MenuItem> create(MenuInput input) {
    throw UnimplementedError();
  }

  @override
  Future<void> delete(int id) {
    throw UnimplementedError();
  }

  @override
  Future<PaginatedResult<MenuItem>> list({
    int page = 1,
    int pageSize = 20,
    String? query,
    int? parentId,
    bool? isActive,
    bool includeDeleted = false,
  }) async {
    return PaginatedResult<MenuItem>(
      items: const [],
      page: 1,
      pageSize: 0,
      total: 0,
    );
  }

  @override
  Future<MenuItem> restore(int id) {
    throw UnimplementedError();
  }

  @override
  Future<MenuItem> update(int id, MenuInput input) {
    throw UnimplementedError();
  }
}
