import 'dart:convert'; import 'package:hive/hive.dart'; import '../../cekirdek/log.dart'; import '../../modeller/video_sihirbaz_sablon.dart'; /// Video sihirbazı için kullanıcı şablonları (Hive box). /// Her şablon: sihirbaz seçimleri + LoRA stack + serbest notlar. class VideoSihirbazSablonServisi { static const _boxAdi = 'wangp_sihirbaz_sablonlari'; static Box? _box; static Future> _kutu() async { _box ??= await Hive.openBox(_boxAdi); return _box!; } static Future> liste() async { final kutu = await _kutu(); final list = []; for (final raw in kutu.values) { try { final j = jsonDecode(raw) as Map; list.add(VideoSihirbazSablon.fromJson(j)); } catch (e) { Log.uyari('VideoSihirbaz', 'parse: $e'); } } list.sort((a, b) => (b.guncelleme ?? b.olusturma ?? DateTime(2000)) .compareTo(a.guncelleme ?? a.olusturma ?? DateTime(2000))); return list; } static Future kaydet(VideoSihirbazSablon s) async { final kutu = await _kutu(); final guncel = s.copyWith( olusturma: s.olusturma ?? DateTime.now(), guncelleme: DateTime.now(), ); await kutu.put(guncel.id, jsonEncode(guncel.toJson())); } static Future sil(String id) async { final kutu = await _kutu(); await kutu.delete(id); } static Future bul(String id) async { final kutu = await _kutu(); final raw = kutu.get(id); if (raw == null) return null; try { return VideoSihirbazSablon.fromJson( jsonDecode(raw) as Map); } catch (_) { return null; } } }