import 'package:dio/dio.dart';
import 'package:niogu_app/core/constants/app_url.dart';
import 'package:niogu_app/core/models/api_response.dart';
import 'package:niogu_app/features/auth/data/models/login_response_model.dart';
import 'package:niogu_app/features/auth/data/models/register_request_model.dart';

abstract class IAuthRemoteDatasource {
  Future<ApiResponseModel<LoginResponseModel>> login({
    required String identifier,
    required String password,
    String? businessCode,
  });

  Future<ApiResponseModel<void>> register(RegisterRequestModel request);
}

class AuthRemoteDatasourceImpl implements IAuthRemoteDatasource {
  final Dio _dio;

  AuthRemoteDatasourceImpl(this._dio);

  @override
  Future<ApiResponseModel<LoginResponseModel>> login({
    required String identifier,
    required String password,
    String? businessCode,
  }) async {
    final response = await _dio.post(
      AppUrl.LOGIN,
      data: {
        'identifier': identifier,
        'password': password,
        'business_code': businessCode,
      },
    );

    return ApiResponseModel<LoginResponseModel>.fromJson(
      response.data,
      (json) => LoginResponseModel.fromJson(json as Map<String, dynamic>),
    );
  }

  @override
  Future<ApiResponseModel<void>> register(RegisterRequestModel request) async {
    final response = await _dio.post(AppUrl.REGISTER, data: request.toJson());

    return ApiResponseModel<void>.fromJson(response.data, (json) => null);
  }
}
