import 'package:dio/dio.dart';
import '../models/dashboard_stats_model.dart';
import '../../../../core/network/dio_client.dart';
import '../../../../core/error/exceptions.dart';

abstract class DashboardRemoteDataSource {
  Future<DashboardDataModel> getDashboardData(String organizationId, String userId);
  Future<DashboardStatsModel> getDashboardStats(String organizationId, String userId);
  Future<List<RecentActivityModel>> getRecentActivities(String organizationId, String userId, {int limit = 10});
  Future<List<UpcomingEventModel>> getUpcomingEvents(String organizationId, String userId, {int limit = 5});
}

class DashboardRemoteDataSourceImpl implements DashboardRemoteDataSource {
  final DioClient dioClient;

  DashboardRemoteDataSourceImpl({required this.dioClient});

  @override
  Future<DashboardDataModel> getDashboardData(String organizationId, String userId) async {
    try {
      final response = await dioClient.get(
        '/api/v1/dashboard/data',
        queryParameters: {
          'organizationId': organizationId,
          'userId': userId,
        },
      );

      if (response.statusCode == 200) {
        return DashboardDataModel.fromJson(response.data);
      } else {
        throw ServerException('Failed to load dashboard data: ${response.statusCode}');
      }
    } on DioException catch (e) {
      throw ServerException('Network error: ${e.message}');
    } catch (e) {
      throw ServerException('Unexpected error: $e');
    }
  }

  @override
  Future<DashboardStatsModel> getDashboardStats(String organizationId, String userId) async {
    try {
      final response = await dioClient.get(
        '/api/v1/dashboard/stats',
        queryParameters: {
          'organizationId': organizationId,
          'userId': userId,
        },
      );

      if (response.statusCode == 200) {
        return DashboardStatsModel.fromJson(response.data);
      } else {
        throw ServerException('Failed to load dashboard stats: ${response.statusCode}');
      }
    } on DioException catch (e) {
      throw ServerException('Network error: ${e.message}');
    } catch (e) {
      throw ServerException('Unexpected error: $e');
    }
  }

  @override
  Future<List<RecentActivityModel>> getRecentActivities(
    String organizationId,
    String userId, {
    int limit = 10,
  }) async {
    try {
      final response = await dioClient.get(
        '/api/v1/dashboard/activities',
        queryParameters: {
          'organizationId': organizationId,
          'userId': userId,
          'limit': limit,
        },
      );

      if (response.statusCode == 200) {
        final List<dynamic> data = response.data['activities'] ?? [];
        return data.map((json) => RecentActivityModel.fromJson(json)).toList();
      } else {
        throw ServerException('Failed to load recent activities: ${response.statusCode}');
      }
    } on DioException catch (e) {
      throw ServerException('Network error: ${e.message}');
    } catch (e) {
      throw ServerException('Unexpected error: $e');
    }
  }

  @override
  Future<List<UpcomingEventModel>> getUpcomingEvents(
    String organizationId,
    String userId, {
    int limit = 5,
  }) async {
    try {
      final response = await dioClient.get(
        '/api/v1/dashboard/events/upcoming',
        queryParameters: {
          'organizationId': organizationId,
          'userId': userId,
          'limit': limit,
        },
      );

      if (response.statusCode == 200) {
        final List<dynamic> data = response.data['events'] ?? [];
        return data.map((json) => UpcomingEventModel.fromJson(json)).toList();
      } else {
        throw ServerException('Failed to load upcoming events: ${response.statusCode}');
      }
    } on DioException catch (e) {
      throw ServerException('Network error: ${e.message}');
    } catch (e) {
      throw ServerException('Unexpected error: $e');
    }
  }
}
