import 'dart:convert'; import 'dart:io'; import 'package:hive/hive.dart'; import 'package:hive_flutter/hive_flutter.dart'; import 'package:path/path.dart' as p; import 'package:path_provider/path_provider.dart'; import '../cekirdek/dosya_yardimcisi.dart'; import '../cekirdek/kimlik.dart'; import '../cekirdek/log.dart'; import '../modeller/proje.dart'; import 'kaynak_kopyalayici.dart'; class ProjeOzeti { final String id; final String isim; final String klasorYolu; final DateTime guncellendi; final String? thumbPath; const ProjeOzeti({ required this.id, required this.isim, required this.klasorYolu, required this.guncellendi, this.thumbPath, }); Map toJson() => { 'id': id, 'isim': isim, 'klasorYolu': klasorYolu, 'guncellendi': guncellendi.toIso8601String(), 'thumbPath': thumbPath, }; static ProjeOzeti fromJson(Map j) => ProjeOzeti( id: j['id'] as String, isim: j['isim'] as String, klasorYolu: j['klasorYolu'] as String, guncellendi: DateTime.parse(j['guncellendi'] as String), thumbPath: j['thumbPath'] as String?, ); } class ProjeDeposu { static Box? _kutu; static Future _box() async => _kutu ??= await Hive.openBox('projeler'); static Future _projelerKok() async { final docs = await getApplicationDocumentsDirectory(); final kok = Directory( p.join(docs.path, 'FikoYT-VideoStudyo', 'projeler'), ); if (!await kok.exists()) await kok.create(recursive: true); return kok; } static Future> sonProjeler() async { final b = await _box(); final liste = []; for (final v in b.values) { try { liste.add(ProjeOzeti.fromJson(Map.from(v as Map))); } catch (e) { Log.uyari('ProjeDeposu', 'Bozuk kayıt atlandı: $e'); } } liste.sort((a, b) => b.guncellendi.compareTo(a.guncellendi)); return liste; } /// Yeni proje oluşturur: klasör yapısı, kaynak dosyaları kopyala, JSON yaz. /// `sesPath` ve `girisGorseliPath` opsiyonel — boş proje oluşturup sonradan /// ses/görsel eklenebilir. static Future projeOlustur({ required String isim, String? sesPath, String? girisGorseliPath, String? faceSwapPath, }) async { final id = FikoYTId.yeni(); final kok = await _projelerKok(); final klasorAd = '${DosyaYardimcisi.guvenliDosyaIsmi(isim)}_${id.substring(0, 8)}'; final klasor = Directory(p.join(kok.path, klasorAd)); await klasor.create(recursive: true); for (final alt in const [ 'kaynak', 'ses_segmentleri', 'kareler', 'segmentler', 'analiz', 'on_izleme', 'cikti', ]) { await Directory(p.join(klasor.path, alt)).create(); } await Log.dosyaBaslat(File(p.join(klasor.path, 'proje.log'))); final kaynak = await KaynakKopyalayici.projeyeKopyala( projeKlasoru: klasor.path, sesPath: sesPath ?? '', girisGorseliPath: girisGorseliPath ?? '', faceSwapPath: faceSwapPath, ); final simdi = DateTime.now(); final proje = Proje( id: id, isim: isim, olusturmaTarihi: simdi, guncellemeTarihi: simdi, klasorYolu: klasor.path, kaynak: kaynak, ); await kaydet(proje); Log.bilgi('ProjeDeposu', 'Yeni proje oluşturuldu: $isim @ ${klasor.path}'); return proje; } static Future kaydet(Proje proje) async { final yeni = proje.copyWith(guncellemeTarihi: DateTime.now()); final dosya = File(p.join(yeni.klasorYolu, 'proje.json')); await DosyaYardimcisi.atomikYaz( dosya, const JsonEncoder.withIndent(' ').convert(yeni.toJson()), ); final b = await _box(); await b.put( yeni.id, ProjeOzeti( id: yeni.id, isim: yeni.isim, klasorYolu: yeni.klasorYolu, guncellendi: yeni.guncellemeTarihi, ).toJson(), ); } static Future yukle(String klasorYolu) async { final dosya = File(p.join(klasorYolu, 'proje.json')); if (!await dosya.exists()) return null; try { final j = jsonDecode(await dosya.readAsString()) as Map; return Proje.fromJson(j); } catch (e) { Log.hata('ProjeDeposu', 'Yükleme hatası: $e'); return null; } } static Future sil(String projeId) async { final b = await _box(); final ozet = b.get(projeId); if (ozet != null) { final klasor = Directory(ozet['klasorYolu'] as String); if (await klasor.exists()) { await klasor.delete(recursive: true); } await b.delete(projeId); } } }