import 'package:autos/core/constants/api_endpoints.dart';
import 'package:autos/data/models/user_model.dart';
import 'package:autos/domain/entities/user.dart';
import 'package:autos/data/sources/remote/api_service.dart';
import 'package:flutter/foundation.dart';
import 'package:dio/dio.dart';

abstract class UserRepository {
  ///Login
  Future<User> login(String email, String password);

  ///Sign up
  Future<User> signup(String name, String email, String password, String phone);
}

class UserRepositoryImpl implements UserRepository {
  final ApiService _apiService;
  UserRepositoryImpl(this._apiService);

  ///Login
  @override
  Future<User> login(String email, String password) async {
    try {
      final response = await _apiService.post(ApiEndpoints.login, {
        "email": email,
        "password": password,
      });

      if (kDebugMode) {
        debugPrint('📦 Login Response: ${response.data}');
      }

      if (response.statusCode == 200) {
        final data = response.data;
        if (data['code'] == 'LOGIN_SUCCESS') {
          return UserModel.fromJson(data);
        } else {
          throw Exception(data['message'] ?? 'Login failed');
        }
      } else {
        throw Exception('Error: ${response.statusMessage}');
      }
    } on DioException catch (e) {
      throw Exception(e.response?.data['message'] ?? 'Network error');
    }
  }

  ///Sign up
  @override
  Future<User> signup(
    String name,
    String email,
    String password,
    String phone,
  ) async {
    try {
      final response = await _apiService.post(ApiEndpoints.signup, {
        "name": name,
        "email": email,
        "password": password,
        "phonenumber": phone,
      });
      if (kDebugMode) {
        debugPrint('📦 Signup Response: ${response.data}');
      }

      if (response.statusCode == 201) {
        final data = response.data;

        if (data['code'] == "USER_CREATED") {
          return UserModel.fromJson(data);
        } else {
          throw Exception(data['message'] ?? 'Signup failed');
        }
      } else {
        throw Exception('Error: ${response.statusMessage}');
      }
    } on DioException catch (e) {
      throw Exception(e.response?.data['message'] ?? 'Network error');
    }
  }

  ///Reset password
  Future<void> sendPasswordResetLink(String email) async {
    try {
      final response = await _apiService.post(ApiEndpoints.forgotPassword, {
        "email": email,
      });

      if (response.statusCode == 200 &&
          response.data['code'] == "RESET_LINK_SENT") {
        return;
      } else {
        throw Exception(
          response.data['message'] ?? "Failed to send reset link",
        );
      }
    } catch (e) {
      throw Exception("Network error: $e");
    }
  }

  ///Userdetails
  Future<UserModel> getUserDetails(String userId) async {
    try {
      final response = await _apiService.get(ApiEndpoints.userDetails + userId);

      if (response.statusCode == 200) {
        final data = response.data;

        if (data["code"] == "USER_FOUND") {
          return UserModel.fromJson(data["user"]);
        } else {
          throw Exception(data["message"] ?? "User not found");
        }
      } else {
        throw Exception("Failed: ${response.statusMessage}");
      }
    } catch (e) {
      throw Exception("Network error: $e");
    }
  }
}
