import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:marco/helpers/services/storage/local_storage.dart';

class AuthService {
  static bool isLoggedIn = false;

  static Future<Map<String, String>?> loginUser(
      Map<String, dynamic> data) async {
    try {
      final response = await http.post(
        Uri.parse('https://api.marcoaiot.com/api/auth/login'),
        headers: {'Content-Type': 'application/json'},
        body: jsonEncode(data),
      );

      if (response.statusCode == 200) {
        isLoggedIn = true;
        await LocalStorage.setLoggedInUser(true);
        // You can also store the user details in local storage if needed
        return null; // No error, login successful
      } else if (response.statusCode == 401) {
        return {"password": "Invalid email or password"};
      } else {
        return {"error": "Something went wrong. Please try again."};
      }
    } catch (e) {
      return {"error": "Network error. Please check your connection."};
    }
  }
}
