import 'dart:io'; import 'package:path_provider/path_provider.dart'; import 'package:path/path.dart' as path; import 'package:uuid/uuid.dart'; import '../database/database_helper.dart'; import '../models/session.dart'; import '../models/shot.dart'; import '../models/target_type.dart'; class SessionRepository { final DatabaseHelper _databaseHelper; final Uuid _uuid; SessionRepository({ DatabaseHelper? databaseHelper, Uuid? uuid, }) : _databaseHelper = databaseHelper ?? DatabaseHelper(), _uuid = uuid ?? const Uuid(); Future createSession({ required TargetType targetType, required String imagePath, required List shots, required int totalScore, double? groupingDiameter, double? groupingCenterX, double? groupingCenterY, String? notes, double? targetCenterX, double? targetCenterY, double? targetRadius, }) async { // Copy image to app documents directory final savedImagePath = await _saveImage(imagePath); final session = Session( id: _uuid.v4(), targetType: targetType, imagePath: savedImagePath, shots: shots, totalScore: totalScore, groupingDiameter: groupingDiameter, groupingCenterX: groupingCenterX, groupingCenterY: groupingCenterY, createdAt: DateTime.now(), notes: notes, targetCenterX: targetCenterX, targetCenterY: targetCenterY, targetRadius: targetRadius, ); await _databaseHelper.insertSession(session); return session; } Future _saveImage(String sourcePath) async { final appDir = await getApplicationDocumentsDirectory(); final imagesDir = Directory(path.join(appDir.path, 'target_images')); if (!await imagesDir.exists()) { await imagesDir.create(recursive: true); } final fileName = '${_uuid.v4()}${path.extension(sourcePath)}'; final destPath = path.join(imagesDir.path, fileName); final sourceFile = File(sourcePath); await sourceFile.copy(destPath); return destPath; } Future getSession(String id) async { return await _databaseHelper.getSession(id); } Future> getAllSessions({ TargetType? targetType, int? limit, int? offset, }) async { return await _databaseHelper.getAllSessions( targetType: targetType?.name, limit: limit, offset: offset, ); } Future updateSession(Session session) async { await _databaseHelper.updateSession(session); } Future deleteSession(String id) async { final session = await getSession(id); if (session != null) { // Delete the image file final imageFile = File(session.imagePath); if (await imageFile.exists()) { await imageFile.delete(); } } await _databaseHelper.deleteSession(id); } Future> getStatistics() async { return await _databaseHelper.getStatistics(); } String generateShotId() { return _uuid.v4(); } }