import 'package:injectable/injectable.dart';
import 'package:superport/core/errors/exceptions.dart';
import 'package:superport/core/errors/failures.dart';
import 'package:superport/data/datasources/remote/maintenance_remote_datasource.dart';
import 'package:superport/data/models/maintenance_dto.dart';
import 'package:superport/data/repositories/maintenance_repository.dart';

@LazySingleton(as: MaintenanceRepository)
class MaintenanceRepositoryImpl implements MaintenanceRepository {
  final MaintenanceRemoteDataSource _remoteDataSource;

  MaintenanceRepositoryImpl({
    required MaintenanceRemoteDataSource remoteDataSource,
  }) : _remoteDataSource = remoteDataSource;

  @override
  Future<MaintenanceListResponse> getMaintenances({
    int page = 1,
    int perPage = 20,
    int? equipmentId,
    String? maintenanceType,
    bool? isExpired,
    int? expiringDays,
    bool includeDeleted = false,
  }) async {
    try {
      return await _remoteDataSource.getMaintenances(
        page: page,
        perPage: perPage,
        equipmentId: equipmentId,
        maintenanceType: maintenanceType,
        isExpired: isExpired,
        expiringDays: expiringDays,
        includeDeleted: includeDeleted,
      );
    } on ServerException catch (e) {
      throw ServerFailure(
        message: e.message,
        statusCode: e.statusCode,
      );
    } catch (e) {
      throw ServerFailure(message: '유지보수 목록 조회 중 오류가 발생했습니다');
    }
  }

  @override
  Future<MaintenanceDto> getMaintenanceDetail(int id) async {
    try {
      return await _remoteDataSource.getMaintenanceDetail(id);
    } on ServerException catch (e) {
      throw ServerFailure(
        message: e.message,
        statusCode: e.statusCode,
      );
    } catch (e) {
      throw ServerFailure(message: '유지보수 상세 조회 중 오류가 발생했습니다');
    }
  }

  @override
  Future<MaintenanceDto> createMaintenance(MaintenanceRequestDto request) async {
    try {
      return await _remoteDataSource.createMaintenance(request);
    } on ServerException catch (e) {
      throw ServerFailure(
        message: e.message,
        statusCode: e.statusCode,
      );
    } catch (e) {
      throw ServerFailure(message: '유지보수 생성 중 오류가 발생했습니다');
    }
  }

  @override
  Future<MaintenanceDto> updateMaintenance(int id, MaintenanceUpdateRequestDto request) async {
    try {
      return await _remoteDataSource.updateMaintenance(id, request);
    } on ServerException catch (e) {
      throw ServerFailure(
        message: e.message,
        statusCode: e.statusCode,
      );
    } catch (e) {
      throw ServerFailure(message: '유지보수 수정 중 오류가 발생했습니다');
    }
  }

  @override
  Future<void> deleteMaintenance(int id) async {
    try {
      return await _remoteDataSource.deleteMaintenance(id);
    } on ServerException catch (e) {
      throw ServerFailure(
        message: e.message,
        statusCode: e.statusCode,
      );
    } catch (e) {
      throw ServerFailure(message: '유지보수 삭제 중 오류가 발생했습니다');
    }
  }

  @override
  Future<List<MaintenanceDto>> getExpiringMaintenances({int days = 30}) async {
    try {
      return await _remoteDataSource.getExpiringMaintenances(days: days);
    } on ServerException catch (e) {
      throw ServerFailure(
        message: e.message,
        statusCode: e.statusCode,
      );
    } catch (e) {
      throw ServerFailure(message: '만료 예정 유지보수 조회 중 오류가 발생했습니다');
    }
  }
}