import 'package:dartz/dartz.dart';
import '../../../services/equipment_service.dart';
import '../../../models/equipment_unified_model.dart';
import '../../../core/errors/failures.dart';
import '../../../data/models/common/paginated_response.dart';
import '../base_usecase.dart';

/// 장비 목록 조회 파라미터
class GetEquipmentsParams {
  final int page;
  final int perPage;
  final String? status;
  final int? companyId;
  final int? warehouseLocationId;
  final String? search;

  const GetEquipmentsParams({
    this.page = 1,
    this.perPage = 20,
    this.status,
    this.companyId,
    this.warehouseLocationId,
    this.search,
  });
}

/// 장비 목록 조회 UseCase
/// 필터링 및 페이지네이션 지원
class GetEquipmentsUseCase extends UseCase<PaginatedResponse<Equipment>, GetEquipmentsParams> {
  final EquipmentService _equipmentService;

  GetEquipmentsUseCase(this._equipmentService);

  @override
  Future<Either<Failure, PaginatedResponse<Equipment>>> call(GetEquipmentsParams params) async {
    try {
      // 상태 유효성 검증
      if (params.status != null && 
          !['available', 'in_use', 'maintenance', 'disposed', 'rented'].contains(params.status)) {
        return Left(ValidationFailure(
          message: '올바르지 않은 장비 상태입니다.',
          errors: {'status': '유효한 상태를 선택해주세요.'},
        ));
      }

      final equipments = await _equipmentService.getEquipments(
        page: params.page,
        perPage: params.perPage,
        status: params.status,
        companyId: params.companyId,
        warehouseLocationId: params.warehouseLocationId,
        search: params.search,
      );
      
      return Right(equipments);
    } catch (e) {
      if (e.toString().contains('네트워크')) {
        return Left(NetworkFailure(
          message: '네트워크 연결을 확인해주세요.',
          originalError: e,
        ));
      } else {
        return Left(ServerFailure(
          message: '장비 목록을 불러오는 중 오류가 발생했습니다.',
          originalError: e,
        ));
      }
    }
  }
}