import 'package:get_it/get_it.dart';
import 'package:superport/core/errors/exceptions.dart';
import 'package:superport/core/errors/failures.dart';
import 'package:superport/data/models/equipment_history_dto.dart';
import 'package:superport/data/models/stock_status_dto.dart';
import 'package:superport/data/repositories/equipment_history_repository.dart';

/// Equipment History Service - 입출고 및 재고 관리 비즈니스 로직 담당
class EquipmentHistoryService {
  final EquipmentHistoryRepository _repository = GetIt.instance<EquipmentHistoryRepository>();

  /// 재고 현황 조회 (핵심 기능)
  Future<List<StockStatusDto>> getStockStatus() async {
    try {
      return await _repository.getStockStatus();
    } on ServerException catch (e) {
      throw ServerFailure(message: e.message);
    } catch (e) {
      throw ServerFailure(message: 'Failed to fetch stock status: $e');
    }
  }

  /// 장비별 이력 조회
  Future<List<EquipmentHistoryDto>> getEquipmentHistoriesByEquipmentId(int equipmentId) async {
    try {
      return await _repository.getEquipmentHistoriesByEquipmentId(equipmentId);
    } on ServerException catch (e) {
      throw ServerFailure(message: e.message);
    } catch (e) {
      throw ServerFailure(message: 'Failed to fetch equipment histories: $e');
    }
  }

  /// 입고 처리
  Future<EquipmentHistoryDto> createStockIn({
    required int equipmentsId,
    required int warehousesId,
    required int quantity,
    DateTime? transactedAt,
    String? remark,
  }) async {
    try {
      return await _repository.createStockIn(
        equipmentsId: equipmentsId,
        warehousesId: warehousesId,
        quantity: quantity,
        transactedAt: transactedAt,
        remark: remark,
      );
    } on ServerException catch (e) {
      throw ServerFailure(message: e.message);
    } catch (e) {
      throw ServerFailure(message: 'Failed to create stock in: $e');
    }
  }

  /// 출고 처리
  Future<EquipmentHistoryDto> createStockOut({
    required int equipmentsId,
    required int warehousesId,
    required int quantity,
    DateTime? transactedAt,
    String? remark,
  }) async {
    try {
      return await _repository.createStockOut(
        equipmentsId: equipmentsId,
        warehousesId: warehousesId,
        quantity: quantity,
        transactedAt: transactedAt,
        remark: remark,
      );
    } on ServerException catch (e) {
      throw ServerFailure(message: e.message);
    } catch (e) {
      throw ServerFailure(message: 'Failed to create stock out: $e');
    }
  }

  /// 장비 이력 생성 (범용)
  Future<EquipmentHistoryDto> createEquipmentHistory(EquipmentHistoryRequestDto request) async {
    try {
      return await _repository.createEquipmentHistory(request);
    } on ServerException catch (e) {
      throw ServerFailure(message: e.message);
    } catch (e) {
      throw ServerFailure(message: 'Failed to create equipment history: $e');
    }
  }

  /// 장비 이력 수정
  Future<EquipmentHistoryDto> updateEquipmentHistory(int id, EquipmentHistoryUpdateRequestDto request) async {
    try {
      return await _repository.updateEquipmentHistory(id, request);
    } on ServerException catch (e) {
      throw ServerFailure(message: e.message);
    } catch (e) {
      throw ServerFailure(message: 'Failed to update equipment history: $e');
    }
  }

  /// 장비 이력 삭제
  Future<void> deleteEquipmentHistory(int id) async {
    try {
      await _repository.deleteEquipmentHistory(id);
    } on ServerException catch (e) {
      throw ServerFailure(message: e.message);
    } catch (e) {
      throw ServerFailure(message: 'Failed to delete equipment history: $e');
    }
  }

  /// 장비 이력 상세 조회
  Future<EquipmentHistoryDto> getEquipmentHistoryById(int id) async {
    try {
      return await _repository.getEquipmentHistoryById(id);
    } on ServerException catch (e) {
      throw ServerFailure(message: e.message);
    } catch (e) {
      throw ServerFailure(message: 'Failed to fetch equipment history detail: $e');
    }
  }
}