import 'package:dartz/dartz.dart';
import '../../../../core/errors/exceptions.dart';
import '../../../../core/errors/failures.dart';
import '../../domain/entities/warehouse_entity.dart';
import '../../domain/repositories/warehouse_repository.dart';
import '../datasources/warehouse_remote_datasource.dart';

/// Implementation of WarehouseRepository
/// Coordinates between data sources and domain layer
/// Converts exceptions to failures for proper error handling
class WarehouseRepositoryImpl implements WarehouseRepository {
  final WarehouseRemoteDataSource remoteDataSource;

  WarehouseRepositoryImpl(this.remoteDataSource);

  @override
  Future<Either<Failure, List<WarehouseEntity>>> getWarehouses() async {
    try {
      // Fetch warehouses from remote data source
      final warehouses = await remoteDataSource.getWarehouses();

      // Convert models to entities
      final entities = warehouses
          .map((model) => model.toEntity())
          .toList();

      return Right(entities);
    } on ServerException catch (e) {
      // Convert server exceptions to server failures
      return Left(ServerFailure(e.message));
    } on NetworkException catch (e) {
      // Convert network exceptions to network failures
      return Left(NetworkFailure(e.message));
    } catch (e) {
      // Handle any unexpected errors
      return Left(ServerFailure('Unexpected error: ${e.toString()}'));
    }
  }
}
