import 'package:aman_kassa_flutter/core/base/base_service.dart';
import 'package:aman_kassa_flutter/core/models/message.dart';
import 'package:aman_kassa_flutter/core/models/response.dart';
import 'package:aman_kassa_flutter/core/models/user.dart';
import 'package:aman_kassa_flutter/core/models/auth_response.dart';
import 'package:flutter/foundation.dart';

import 'ApiService.dart';

class AuthenticationService extends BaseService {
  final ApiService _api;

  AuthenticationService({required ApiService api}) : _api = api;

  User? _currentUser;
  User? get currentUser => _currentUser;

  Future<AuthBody?> loginWithEmail({
    required String email,
    required String password,
  }) async {
    try {
      AuthBody? result = await _api.authenticate(email, password);
      if (result!=null && result.user != null) {
        _currentUser = result.user;
      }
      return result;
    } catch (e) {
      return null;
    }
  }

  Future<bool> isUserLoggedIn(String token) async {
    Response<Message> session = await _api.isActive(token);
    if ("OK" == session.body?.message) {
      //_session = session;
      return true;
    }
    return false;
  }

  Future<bool> logout(String token) async {
    Response<dynamic> session = await _api.logout(token);
    if ("logout" == session.body.message) {
      return true;
    }
    return false;
  }
}
