import 'api_client.dart';

class TeamMember {
  final String id;
  final String firstName;
  final String lastName;
  final String email;
  final String role;
  final bool isActive;
  final DateTime createdAt;

  const TeamMember({
    required this.id,
    required this.firstName,
    required this.lastName,
    required this.email,
    required this.role,
    required this.isActive,
    required this.createdAt,
  });

  String get fullName => '$firstName $lastName';

  factory TeamMember.fromJson(Map<String, dynamic> json) => TeamMember(
        id: json['id'] as String,
        firstName: json['firstName'] as String,
        lastName: json['lastName'] as String,
        email: json['email'] as String,
        role: json['role'] as String? ?? 'AGENT',
        isActive: json['isActive'] as bool? ?? true,
        createdAt: DateTime.parse(
            json['createdAt'] as String? ?? DateTime.now().toIso8601String()),
      );
}

class TeamService {
  final _client = ApiClient.instance;

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

  Future<void> inviteMember(Map<String, dynamic> data) async {
    await _client.post('/team/invite', data: data);
  }

  Future<void> updateRole(String id, String role) async {
    await _client.patch('/team/$id/role', data: {'role': role});
  }

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

  Future<void> reactivateMember(String id) async {
    await _client.post('/team/$id/reactivate');
  }
}
