import 'package:shared_preferences/shared_preferences.dart';

import '../shared/models/user.dart';

class AuthStorage {
  static const _keyAccess = 'auth_access_token';
  static const _keyRefresh = 'auth_refresh_token';
  static const _keyUserId = 'auth_user_id';
  static const _keyUserEmail = 'auth_user_email';
  static const _keyUserName = 'auth_user_name';
  static const _keyUserRole = 'auth_user_role';

  static Future<({User user, String accessToken, String refreshToken})?> load() async {
    final prefs = await SharedPreferences.getInstance();
    final access = prefs.getString(_keyAccess);
    final refresh = prefs.getString(_keyRefresh);
    final userId = prefs.getString(_keyUserId);
    final email = prefs.getString(_keyUserEmail);
    final name = prefs.getString(_keyUserName);
    if (access == null ||
        access.isEmpty ||
        refresh == null ||
        refresh.isEmpty ||
        userId == null ||
        email == null ||
        name == null) {
      return null;
    }
    return (
      user: User(
        id: userId,
        email: email,
        name: name,
        role: prefs.getString(_keyUserRole) ?? 'coach',
      ),
      accessToken: access,
      refreshToken: refresh,
    );
  }

  static Future<void> save({
    required User user,
    required String accessToken,
    required String refreshToken,
  }) async {
    final prefs = await SharedPreferences.getInstance();
    await prefs.setString(_keyAccess, accessToken);
    await prefs.setString(_keyRefresh, refreshToken);
    await prefs.setString(_keyUserId, user.id);
    await prefs.setString(_keyUserEmail, user.email);
    await prefs.setString(_keyUserName, user.name);
    await prefs.setString(_keyUserRole, user.role);
  }

  static Future<void> clear() async {
    final prefs = await SharedPreferences.getInstance();
    await prefs.remove(_keyAccess);
    await prefs.remove(_keyRefresh);
    await prefs.remove(_keyUserId);
    await prefs.remove(_keyUserEmail);
    await prefs.remove(_keyUserName);
    await prefs.remove(_keyUserRole);
  }
}
