import 'package:dio/dio.dart';
import 'package:niogu_ecommerce_v1/core/errors/exceptions.dart';
import 'package:niogu_ecommerce_v1/core/utils/log_message.dart';
import 'package:niogu_ecommerce_v1/features/auth/data/datasource/auth_remote_datasource_impl.dart';
import 'package:niogu_ecommerce_v1/features/auth/data/models/auth_model.dart';
import 'package:niogu_ecommerce_v1/features/auth/domain/repositories/i_auth_repository.dart';

class AuthRepositoryImpl implements IAuthRepository {
  final IAuthRemoteDatasource _datasource;

  const AuthRepositoryImpl(this._datasource);

  @override
  Future<LoginResponse?> login({
    required String identifier,
    required String password,
  }) async {
    try {
      final response = await _datasource.login(
        identifier: identifier,
        password: password,
      );

      final data = response.data;

      if (response.status != 'success' || data == null) return null;

      return data;
    } on DioException catch (e, st) {
      LogMessage.log.i(e.toString(), error: e, stackTrace: st);
      throw ServerException("Akun tidak ditemukan");
    }
  }

  @override
  Future<void> register(RegisterRequest request) async {
    try {
      await _datasource.register(request);
    } on DioException catch (e, st) {
      LogMessage.log.i(e.toString(), error: e, stackTrace: st);
      throw ServerException("Akun tidak ditemukan");
    }
  }
}
