import 'package:rasadyar_auth/data/models/response/user_info/user_info_model.dart';
import 'package:rasadyar_auth/data/models/response/user_profile_model/user_profile_model.dart';
import 'package:rasadyar_core/core.dart';

import '../models/response/auth/auth_response_model.dart';
import '../models/response/captcha/captcha_response_model.dart';
import 'auth_repository.dart';

class AuthRepositoryImpl implements AuthRepository {
  final DioRemote _httpClient;
  final String _BASE_URL = 'auth/api/v1/';

  AuthRepositoryImpl(this._httpClient);

  @override
  Future<UserProfileModel?> login({
    required Map<String, dynamic> authRequest,
  }) async {
      var res = await _httpClient.post<UserProfileModel?>(
      '/api/login/',
      data: authRequest,
      fromJson: UserProfileModel.fromJson,
      headers: {'Content-Type': 'application/json'},
    );
    return res.data;
  }

  @override
  Future<CaptchaResponseModel?> captcha() async {
    var res = await _httpClient.post<CaptchaResponseModel?>(
      'captcha/',
      fromJson: CaptchaResponseModel.fromJson,
    );
    return res.data;
  }

  @override
  Future<AuthResponseModel?> loginWithRefreshToken({
    required Map<String, dynamic> authRequest,
  }) async {
    var res = await _httpClient.post<AuthResponseModel>(
      '$_BASE_URL/login/',
      data: authRequest,
      headers: {'Content-Type': 'application/json'},
    );
    return res.data;
  }

  @override
  Future<void> logout() {
    // TODO: implement logout
    throw UnimplementedError();
  }

  @override
  Future<bool> hasAuthenticated() async {
    final response = await _httpClient.get<bool>(
      '$_BASE_URL/login/',
      headers: {'Content-Type': 'application/json'},
    );

    return response.data ?? false;
  }

  @override
  Future<UserInfoModel?> getUserInfo(String phoneNumber) async {
    var res = await _httpClient.post<UserInfoModel?>(
      'https://userbackend.rasadyaar.ir/api/send_otp/',
      data: {
        "mobile": phoneNumber,
        "state": ""
      },
      fromJson: UserInfoModel.fromJson,
      headers: {'Content-Type': 'application/json'},
    );
    return res.data;

  }
}
