import '../blocs/organization/organization_state.dart';
import 'api_client.dart';
import 'dart:developer' as developer;

class OrgApi {
  final ApiClient _apiClient;

  OrgApi(this._apiClient);

  Future<List<Organization>> getOrganizations() async {
    return await _apiClient.getList(
      '/orgs',
      fromJson: (data) => Organization.fromJson(data),
    );
  }

  Future<Organization> createOrganization(String name) async {
    developer.log('POST /orgs with payload: {"name": "$name"}', name: 'OrgApi');

    try {
      final result = await _apiClient.post(
        '/orgs',
        data: {'name': name},
        fromJson: (data) => Organization.fromJson(data),
      );

      return result;
    } catch (e) {
      rethrow;
    }
  }
}
