import 'package:dio/dio.dart';
import 'package:kmobile/data/models/transaction.dart';

abstract class TransactionRepository {
  Future<List<Transaction>> fetchTransactions(String accountNo);
}

class TransactionRepositoryImpl implements TransactionRepository {
  final Dio _dio;
  TransactionRepositoryImpl(this._dio);

  @override
  Future<List<Transaction>> fetchTransactions(String accountNo) async {

    final resp = await _dio.get('/api/transactions/account/$accountNo');

    if (resp.statusCode != 200) {
      throw Exception(
        'Error fetching transactions: ${resp.statusCode} ${resp.statusMessage}',
      );
    }

    final List<dynamic> data = resp.data as List<dynamic>;
    return data
        .map((e) => Transaction.fromJson(e as Map<String, dynamic>))
        .toList();
  }
}