import 'package:dio/dio.dart';
import 'package:get_it/get_it.dart';
import 'package:superport/core/constants/api_endpoints.dart';
import 'package:superport/core/errors/exceptions.dart';
import 'package:superport/data/datasources/remote/api_client.dart';
import 'package:superport/data/models/equipment/equipment_dto.dart';

abstract class EquipmentRemoteDataSource {
  Future<EquipmentListResponse> getEquipments({
    int page = 1,
    int perPage = 20,
    String? search,
  });
  
  Future<EquipmentDto> createEquipment(EquipmentRequestDto request);
  
  Future<EquipmentDto> getEquipmentDetail(int id);
  
  Future<EquipmentDto> updateEquipment(int id, EquipmentUpdateRequestDto request);
  
  Future<void> deleteEquipment(int id);
}

class EquipmentRemoteDataSourceImpl implements EquipmentRemoteDataSource {
  final ApiClient _apiClient = GetIt.instance<ApiClient>();

  @override
  Future<EquipmentListResponse> getEquipments({
    int page = 1,
    int perPage = 20,
    String? search,
  }) async {
    try {
      final queryParams = <String, dynamic>{
        'page': page,
        'page_size': perPage,
        if (search != null && search.isNotEmpty) 'search': search,
      };

      final response = await _apiClient.get(
        ApiEndpoints.equipment,
        queryParameters: queryParams,
      );

      print('[Equipment API] Response: ${response.data}');

      // 백엔드 응답은 직접 data 배열과 페이지네이션 정보 반환
      final Map<String, dynamic> responseData = response.data;
      return EquipmentListResponse.fromJson(responseData);
    } on DioException catch (e) {
      throw ServerException(
        message: e.response?.data['message'] ?? 'Network error occurred',
        statusCode: e.response?.statusCode,
      );
    }
  }

  @override
  Future<EquipmentDto> createEquipment(EquipmentRequestDto request) async {
    try {
      final response = await _apiClient.post(
        ApiEndpoints.equipment,
        data: request.toJson(),
      );

      print('[Equipment API] Create Response: ${response.data}');
      return EquipmentDto.fromJson(response.data);
    } on DioException catch (e) {
      throw ServerException(
        message: e.response?.data['message'] ?? 'Network error occurred',
        statusCode: e.response?.statusCode,
      );
    }
  }

  @override
  Future<EquipmentDto> getEquipmentDetail(int id) async {
    try {
      final response = await _apiClient.get('${ApiEndpoints.equipment}/$id');

      print('[Equipment API] Detail Response: ${response.data}');
      return EquipmentDto.fromJson(response.data);
    } on DioException catch (e) {
      throw ServerException(
        message: e.response?.data['message'] ?? 'Network error occurred',
        statusCode: e.response?.statusCode,
      );
    }
  }

  @override
  Future<EquipmentDto> updateEquipment(int id, EquipmentUpdateRequestDto request) async {
    try {
      final response = await _apiClient.put(
        '${ApiEndpoints.equipment}/$id',
        data: request.toJson(),
      );

      print('[Equipment API] Update Response: ${response.data}');
      return EquipmentDto.fromJson(response.data);
    } on DioException catch (e) {
      throw ServerException(
        message: e.response?.data['message'] ?? 'Network error occurred',
        statusCode: e.response?.statusCode,
      );
    }
  }

  @override
  Future<void> deleteEquipment(int id) async {
    try {
      await _apiClient.delete('${ApiEndpoints.equipment}/$id');
    } on DioException catch (e) {
      throw ServerException(
        message: e.response?.data['message'] ?? 'Network error occurred',
        statusCode: e.response?.statusCode,
      );
    }
  }
}