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';

class ManufacturerService {

  Future<List<dynamic>> fetchManufacturers() async {
    final uri = Uri.parse('${ApiService.baseUrl}/InvMainAPI/ManufacturerList');
    debugPrint("Fetching manufacturers from: $uri");

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

      if (response.statusCode == 200) {
        if (response.body.isNotEmpty) {
          return jsonDecode(response.body);
        } else {
          return [];
        }
      } else {
        throw Exception(
            'Failed to load manufacturers. Status code: ${response.statusCode}');
      }
    } on SocketException {
      throw Exception('No Internet connection. Please check your network.');
    } catch (e) {
      debugPrint('Error fetching manufacturers: $e');
      rethrow;
    }
  }


  Future<void> addManufacturer(String manufacturerName) async {
    final uri = Uri.parse('${ApiService.baseUrl}/InvMainAPI/AddManufacturer');
    debugPrint("Adding manufacturer to: $uri");

    try {
      final response = await http.post(
        uri,
        headers: {'Content-Type': 'application/json; charset=UTF-8'},
        body: jsonEncode({'ManufacturerName': manufacturerName}),
      );

      if (response.statusCode != 200) {
        throw Exception(
            'Failed to add manufacturer. Status code: ${response.statusCode}');
      }
    } on SocketException {
      throw Exception('No Internet connection. Please check your network.');
    } catch (e) {
      debugPrint('Error adding manufacturer: $e');
      rethrow;
    }
  }

  Future<void> updateManufacturer(int id, String manufacturerName) async {
    final uri = Uri.parse('${ApiService.baseUrl}/InvMainAPI/EditManufacturer');
    debugPrint("Updating manufacturer at: $uri");

    try {
      final response = await http.post(
        uri,
        headers: {'Content-Type': 'application/json; charset=UTF-8'},
        body: jsonEncode({
          'ManufacturerId': id,
          'ManufacturerName': manufacturerName,
        }),
      );

      if (response.statusCode != 200) {
        throw Exception(
            'Failed to update manufacturer. Status code: ${response.statusCode}');
      }
    } on SocketException {
      throw Exception('No Internet connection. Please check your network.');
    } catch (e) {
      debugPrint('Error updating manufacturer: $e');
      rethrow;
    }
  }

  Future<void> deleteManufacturer(int id) async {
    final uri = Uri.parse('${ApiService.baseUrl}/InvMainAPI/DeleteManufacturer/$id');
    debugPrint("Deleting manufacturer at: $uri");

    try {
      final response = await http.delete(uri);

      if (response.statusCode != 200) {
        throw Exception(
            'Failed to delete manufacturer. Status code: ${response.statusCode}');
      }
    } on SocketException {
      throw Exception('No Internet connection. Please check your network.');
    } catch (e) {
      debugPrint('Error deleting manufacturer: $e');
      rethrow;
    }
  }
}