import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

enum TransactionStatus {
  pending,
  completed,
  failed,
}

class Transaction {
  final String id;
  final String title;
  final int amount;
  final int points;
  final DateTime date;
  final String category;
  final IconData icon;
  final TransactionStatus status;

  Transaction({
    required this.id,
    required this.title,
    required this.amount,
    required this.points,
    required this.date,
    required this.category,
    required this.icon,
    required this.status,
  });

  factory Transaction.fromJson(Map<String, dynamic> json) {
    IconData getIconForCategory(String category) {
      switch (category) {
        case 'Viện thông':
          return Icons.phone_android;
        case 'Mua sắm':
          return Icons.shopping_bag;
        case 'Ưu đãi':
          return Icons.card_giftcard;
        case 'Hóa đơn':
          return Icons.receipt;
        default:
          return Icons.receipt_long;
      }
    }

    TransactionStatus getStatus(String statusStr) {
      switch (statusStr) {
        case 'completed':
          return TransactionStatus.completed;
        case 'pending':
          return TransactionStatus.pending;
        case 'failed':
          return TransactionStatus.failed;
        default:
          return TransactionStatus.pending;
      }
    }

    return Transaction(
      id: json['id'],
      title: json['title'],
      amount: json['amount'],
      points: json['points'],
      date: DateTime.parse(json['date']),
      category: json['category'],
      icon: getIconForCategory(json['category']),
      status: getStatus(json['status']),
    );
  }
}

class TransactionService {
  static const String baseUrl = 'https://api.example.com';

  Future<List<Transaction>> getTransactions({
    required String category,
    required DateTime month,
  }) async {
    try {
      final response = await http.get(
        Uri.parse(
          '$baseUrl/transactions?category=${Uri.encodeComponent(category)}&month=${month.year}-${month.month}',
        ),
        headers: {'Content-Type': 'application/json'},
      );

      if (response.statusCode == 200) {
        final List<dynamic> data = json.decode(response.body);
        return data.map((json) => Transaction.fromJson(json)).toList();
      } else {
        throw Exception('Failed to load transactions: ${response.statusCode}');
      }
    } catch (e) {
      // Trong môi trường thực tế, bạn nên xử lý lỗi một cách phù hợp
      print('Error fetching transactions: $e');
      
      // Trả về dữ liệu mẫu cho mục đích demo
      if (category == 'Ưu đãi') {
        return [
          Transaction(
            id: '1',
            title: 'Thanh toán mua ưu đãi',
            amount: 0,
            points: 0,
            date: DateTime.now(),
            category: 'Ưu đãi',
            icon: Icons.card_giftcard,
            status: TransactionStatus.completed,
          ),
          Transaction(
            id: '2',
            title: 'Thanh toán mua ưu đãi',
            amount: 0,
            points: 0,
            date: DateTime.now(),
            category: 'Ưu đãi',
            icon: Icons.card_giftcard,
            status: TransactionStatus.completed,
          ),
        ];
      }
      return [];
    }
  }
}