import 'package:injectable/injectable.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/license/license_dto.dart'; import 'package:superport/data/models/license/license_request_dto.dart'; import 'package:superport/data/models/license/license_query_dto.dart'; abstract class LicenseRemoteDataSource { Future getLicenses({ int page = 1, int perPage = 20, bool? isActive, int? companyId, int? assignedUserId, String? licenseType, }); Future getLicenseById(int id); Future createLicense(CreateLicenseRequest request); Future updateLicense(int id, UpdateLicenseRequest request); Future deleteLicense(int id); Future assignLicense(int id, AssignLicenseRequest request); Future unassignLicense(int id); Future getExpiringLicenses({ int days = 30, int page = 1, int perPage = 20, }); } @LazySingleton(as: LicenseRemoteDataSource) class LicenseRemoteDataSourceImpl implements LicenseRemoteDataSource { final ApiClient _apiClient; LicenseRemoteDataSourceImpl({ required ApiClient apiClient, }) : _apiClient = apiClient; @override Future getLicenses({ int page = 1, int perPage = 20, bool? isActive, int? companyId, int? assignedUserId, String? licenseType, }) async { try { final queryParams = { 'page': page, 'per_page': perPage, }; if (isActive != null) queryParams['is_active'] = isActive; if (companyId != null) queryParams['company_id'] = companyId; if (assignedUserId != null) queryParams['assigned_user_id'] = assignedUserId; if (licenseType != null) queryParams['license_type'] = licenseType; final response = await _apiClient.get( ApiEndpoints.licenses, queryParameters: queryParams, ); return LicenseListResponseDto.fromJson(response.data); } catch (e) { throw _handleError(e); } } @override Future getLicenseById(int id) async { try { final response = await _apiClient.get( '${ApiEndpoints.licenses}/$id', ); return LicenseDto.fromJson(response.data); } catch (e) { throw _handleError(e); } } @override Future createLicense(CreateLicenseRequest request) async { try { final response = await _apiClient.post( ApiEndpoints.licenses, data: request.toJson(), ); return LicenseDto.fromJson(response.data); } catch (e) { throw _handleError(e); } } @override Future updateLicense(int id, UpdateLicenseRequest request) async { try { final response = await _apiClient.put( '${ApiEndpoints.licenses}/$id', data: request.toJson(), ); return LicenseDto.fromJson(response.data); } catch (e) { throw _handleError(e); } } @override Future deleteLicense(int id) async { try { await _apiClient.delete( '${ApiEndpoints.licenses}/$id', ); } catch (e) { throw _handleError(e); } } @override Future assignLicense(int id, AssignLicenseRequest request) async { try { final response = await _apiClient.patch( '${ApiEndpoints.licenses}/$id/assign', data: request.toJson(), ); return LicenseDto.fromJson(response.data); } catch (e) { throw _handleError(e); } } @override Future unassignLicense(int id) async { try { final response = await _apiClient.patch( '${ApiEndpoints.licenses}/$id/unassign', ); return LicenseDto.fromJson(response.data); } catch (e) { throw _handleError(e); } } @override Future getExpiringLicenses({ int days = 30, int page = 1, int perPage = 20, }) async { try { final queryParams = { 'days': days, 'page': page, 'per_page': perPage, }; final response = await _apiClient.get( '${ApiEndpoints.licenses}/expiring', queryParameters: queryParams, ); return ExpiringLicenseListDto.fromJson(response.data); } catch (e) { throw _handleError(e); } } Exception _handleError(dynamic error) { if (error is ApiException) { return error; } return ApiException( message: error.toString(), ); } }