import 'dart:convert'; import 'package:shared_preferences/shared_preferences.dart'; import '../models/breed_setting.dart'; import '../models/reservation.dart'; import '../models/customer.dart'; class StorageService { static const String breedsKey = 'breed_settings_v2'; static const String reservationsKey = 'reservations_v2'; static const String customersKey = 'customers_v1'; static const String warehouseStockKey = 'warehouse_stock_v1'; static const String lastProductionUpdateKey = 'last_production_update_v1'; static Future> loadWarehouseStock() async { final prefs = await SharedPreferences.getInstance(); final raw = prefs.getString(warehouseStockKey); if (raw == null || raw.isEmpty) return {}; final map = jsonDecode(raw) as Map; return map.map((k, v) => MapEntry(k, (v as num).toDouble())); } static Future saveWarehouseStock(Map stock) async { final prefs = await SharedPreferences.getInstance(); await prefs.setString(warehouseStockKey, jsonEncode(stock)); } static Future loadLastProductionUpdate() async { final prefs = await SharedPreferences.getInstance(); final raw = prefs.getString(lastProductionUpdateKey); if (raw == null || raw.isEmpty) return null; return DateTime.parse(raw); } static Future saveLastProductionUpdate(DateTime date) async { final prefs = await SharedPreferences.getInstance(); await prefs.setString(lastProductionUpdateKey, date.toIso8601String()); } static Future> loadBreedSettings() async { final prefs = await SharedPreferences.getInstance(); final raw = prefs.getString(breedsKey); if (raw == null || raw.isEmpty) return []; final list = jsonDecode(raw) as List; return list.map((e) => BreedSetting.fromMap(Map.from(e))).toList(); } static Future saveBreedSettings(List items) async { final prefs = await SharedPreferences.getInstance(); await prefs.setString(breedsKey, jsonEncode(items.map((e) => e.toMap()).toList())); } static Future> loadReservations() async { final prefs = await SharedPreferences.getInstance(); final raw = prefs.getString(reservationsKey); if (raw == null || raw.isEmpty) return []; final list = jsonDecode(raw) as List; return list.map((e) => Reservation.fromMap(Map.from(e))).toList(); } static Future saveReservations(List items) async { final prefs = await SharedPreferences.getInstance(); await prefs.setString(reservationsKey, jsonEncode(items.map((e) => e.toMap()).toList())); } static Future> loadCustomers() async { final prefs = await SharedPreferences.getInstance(); final raw = prefs.getString(customersKey); if (raw == null || raw.isEmpty) return []; final list = jsonDecode(raw) as List; return list.map((e) => Customer.fromMap(Map.from(e))).toList(); } static Future saveCustomers(List items) async { final prefs = await SharedPreferences.getInstance(); await prefs.setString(customersKey, jsonEncode(items.map((e) => e.toMap()).toList())); } }