import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart'; // Wajib import ini

class ApiService {
  static String get baseUrl {
    if (kIsWeb) {
      // Jika di Chrome/Web, tembak localhost langsung
      return 'http://localhost:8080/api';
    } else {
      // Jika di Emulator Android, tembak IP khusus 10.0.2.2
      return 'http://10.0.2.2:8080/api';
    }
  }

  final Dio _dio = Dio(BaseOptions(
    baseUrl: baseUrl,
    connectTimeout: const Duration(seconds: 5),
    // Penting buat Web agar tidak kena error CORS di sisi Client
    headers: {
      'Content-Type': 'application/json',
      'Accept': 'application/json',
    },
  ));

  Future<Response> post(String path, Map<String, dynamic> data) async {
    return await _dio.post(path, data: data);
  }
}