import '../models/offer.dart';
import 'api_client.dart';

class OfferService {
  final _client = ApiClient.instance;

  Future<List<CompanyOffer>> getOffers() async {
    final res = await _client.get('/offers');
    final list = res.data as List<dynamic>;
    return list
        .map((e) => CompanyOffer.fromJson(e as Map<String, dynamic>))
        .toList();
  }

  Future<CompanyOffer> getOffer(String id) async {
    final res = await _client.get('/offers/$id');
    return CompanyOffer.fromJson(res.data as Map<String, dynamic>);
  }

  Future<CompanyOffer> createOffer(Map<String, dynamic> data) async {
    final res = await _client.post('/offers', data: data);
    return CompanyOffer.fromJson(res.data as Map<String, dynamic>);
  }

  Future<CompanyOffer> updateOffer(String id, Map<String, dynamic> data) async {
    final res = await _client.patch('/offers/$id', data: data);
    return CompanyOffer.fromJson(res.data as Map<String, dynamic>);
  }

  Future<void> deleteOffer(String id) async {
    await _client.delete('/offers/$id');
  }

  Future<void> activateOffer(String id) async {
    await _client.post('/offers/$id/activate');
  }

  Future<void> deactivateOffer(String id) async {
    await _client.post('/offers/$id/deactivate');
  }
}
