import 'dart:convert';
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:http/http.dart' as http;
import 'package:inventory_system/services/api_service.dart';
import 'package:inventory_system/services/session_manager.dart';

class ReportService {
  Future<Map<String, dynamic>> fetchInventoryReport(int deptId) async {
    final uri = Uri.parse('${ApiService.baseUrl}/InvMainAPI/GetInventoryReport/$deptId');
    final cookie = SessionManager.instance.getCookie();
    debugPrint("Fetching inventory report from: $uri");

    final Map<String, String> headers = {
      'Content-Type': 'application/json; charset=UTF-8',
      if (cookie != null) 'Cookie': cookie,
    };

    try {
      final response = await http.post(uri, headers: headers);

      if (response.statusCode == 200) {
        return response.body.isNotEmpty ? jsonDecode(response.body) : {};
      } else {
        throw Exception('Failed to load inventory report. Status: ${response.statusCode}, Body: ${response.body}');
      }
    } on SocketException {
      throw Exception('No Internet connection.');
    } catch (e) {
      debugPrint('Error fetching inventory report: $e');
      rethrow;
    }
  }
}
