import 'package:dio/dio.dart';
import '../../../../core/api_service.dart';
import 'auth_model.dart';

abstract class AuthRemoteDataSource {
  Future<AuthModel> login(String email, String password);
}

class AuthRemoteDataSourceImpl implements AuthRemoteDataSource {
  final ApiService apiService;

  AuthRemoteDataSourceImpl(this.apiService);

  @override
  Future<AuthModel> login(String email, String password) async {
    try {
      final response = await apiService.post('/auth/login', {
        'email': email,
        'password': password,
      });
      
      // Karena API response lu ada "success": true
      if (response.statusCode == 200 && response.data['success'] == true) {
        return AuthModel.fromJson(response.data);
      } else {
        throw Exception(response.data['message'] ?? 'Gagal login');
      }
    } on DioException catch (e) {
      // Nangkep error dari backend (misal status 400/401)
      throw Exception(e.response?.data['message'] ?? 'Terjadi kesalahan jaringan');
    }
  }
}