import 'api_client.dart';

class Payment {
  final String id;
  final int amount;
  final String currency;
  final String type;
  final String paymentMethod;
  final String status;
  final DateTime createdAt;

  const Payment({
    required this.id,
    required this.amount,
    required this.currency,
    required this.type,
    required this.paymentMethod,
    required this.status,
    required this.createdAt,
  });

  factory Payment.fromJson(Map<String, dynamic> json) => Payment(
        id: json['id'] as String,
        amount: json['amount'] as int? ?? 0,
        currency: json['currency'] as String? ?? 'USD',
        type: json['type'] as String? ?? 'CHARGE',
        paymentMethod: json['paymentMethod'] as String? ?? 'CASH',
        status: json['status'] as String? ?? 'COMPLETED',
        createdAt: DateTime.parse(
            json['createdAt'] as String? ?? DateTime.now().toIso8601String()),
      );
}

class PaymentService {
  final _client = ApiClient.instance;

  Future<List<Payment>> getPayments(String reservationId) async {
    final res = await _client.get('/payments/reservations/$reservationId');
    final list = res.data as List<dynamic>;
    return list
        .map((e) => Payment.fromJson(e as Map<String, dynamic>))
        .toList();
  }

  Future<Payment> recordManualPayment(
      String reservationId, Map<String, dynamic> data) async {
    final res = await _client.post(
        '/payments/reservations/$reservationId/manual',
        data: data);
    return Payment.fromJson(res.data as Map<String, dynamic>);
  }
}
