import 'dart:convert';
import 'dart:io';
import 'package:dio/dio.dart';
import 'package:english_learning/core/services/dio_client.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';

class UserRepository {
  final DioClient dioClient = DioClient();
  final FlutterSecureStorage _secureStorage = const FlutterSecureStorage();

  // Authentication methods
  Future<Response> registerUser(Map<String, dynamic> data) async {
    return await dioClient.registerStudent(data);
  }

  Future<Response> loginUser(Map<String, dynamic> data) async {
    return await dioClient.loginStudent(data);
  }

  Future<Response> logoutUser() async {
    return await dioClient.post('/api/logout');
  }

  Future<Response> forgotPassword(String email) async {
    return await dioClient.forgotPassword({'EMAIL': email});
  }

  // User data methods
  Future<Response> getMe(String token) async {
    return await dioClient.getMe(token);
  }

  Future<Response> updateUserProfile(
    String id,
    Map<String, dynamic> data,
    String token, {
    File? imageFile,
  }) async {
    try {
      FormData formData = FormData.fromMap(data);
      if (imageFile != null) {
        formData.files.add(MapEntry(
          'PICTURE',
          await MultipartFile.fromFile(imageFile.path,
              filename: imageFile.path.split('/').last),
        ));
      }
      return await dioClient.updateUserProfile(id, formData, token);
    } catch (e) {
      print('Update Profile error: $e');
      rethrow;
    }
  }

  Future<Response> updatePassword(
    String id,
    String oldPassword,
    String newPassword,
    String confirmPassword,
    String token,
  ) async {
    try {
      final data = {
        "OLD_PASSWORD": oldPassword,
        "PASSWORD": newPassword,
        "CONFIRM_PASSWORD": confirmPassword
      };
      return await dioClient.updatePassword(id, data, token);
    } catch (e) {
      print('Update Password error: $e');
      rethrow;
    }
  }

  Future<Response> reportIssue(String report, String token) async {
    return await dioClient.reportIssue({'REPORTS': report}, token);
  }

  // Token management methods
  Future<void> saveRefreshToken(String refreshToken) async {
    await _secureStorage.write(key: 'refreshToken', value: refreshToken);
  }

  Future<String?> getRefreshToken() async {
    return await _secureStorage.read(key: 'refreshToken');
  }

  Future<void> deleteRefreshToken() async {
    await _secureStorage.delete(key: 'refreshToken');
  }

  Future<void> saveToken(String token) async {
    await _secureStorage.write(key: 'jwtToken', value: token);
  }

  Future<String?> getToken() async {
    return await _secureStorage.read(key: 'jwtToken');
  }

  Future<void> deleteToken() async {
    await _secureStorage.delete(key: 'jwtToken');
  }

  // User data storage methods
  Future<void> saveUserData(Map<String, dynamic> userData) async {
    await _secureStorage.write(key: 'userData', value: json.encode(userData));
  }

  Future<Map<String, dynamic>?> getUserData() async {
    String? cachedUserData = await _secureStorage.read(key: 'userData');
    return cachedUserData != null
        ? Map<String, dynamic>.from(json.decode(cachedUserData))
        : null;
  }

  Future<void> deleteUserData() async {
    await _secureStorage.delete(key: 'userData');
  }
}
