import 'package:dio/dio.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

import '../../../core/network/api_client.dart';
import '../models/profile_user.dart';

final profileServiceProvider = Provider<ProfileService>((ref) {
  return ProfileService(ref.read(apiClientProvider));
});

class ProfileService {
  ProfileService(this._dio);
  final Dio _dio;

  Future<ProfileUser> getMe() async {
    final res = await _dio.get<Map<String, dynamic>>('/users/me');
    return ProfileUser.fromJson(res.data!);
  }

  Future<void> updateMe({String? name, String? email, String? phone}) async {
    final payload = <String, dynamic>{};
    if (name != null) payload['name'] = name;
    if (email != null) payload['email'] = email;
    if (phone != null) payload['phone'] = phone;
    if (payload.isEmpty) return;
    await _dio.patch<void>('/users/me', data: payload);
  }

  Future<void> changePassword({
    required String currentPassword,
    required String newPassword,
  }) async {
    await _dio.post<void>(
      '/users/me/change-password',
      data: {'current_password': currentPassword, 'new_password': newPassword},
    );
  }
}
