import 'package:riverpod_annotation/riverpod_annotation.dart'; import 'package:trainhub_flutter/injection.dart'; import 'package:trainhub_flutter/domain/entities/annotation.dart'; import 'package:trainhub_flutter/domain/repositories/analysis_repository.dart'; import 'package:trainhub_flutter/domain/repositories/exercise_repository.dart'; import 'package:trainhub_flutter/presentation/analysis/analysis_state.dart'; part 'analysis_controller.g.dart'; @riverpod class AnalysisController extends _$AnalysisController { late AnalysisRepository _repo; late ExerciseRepository _exerciseRepo; @override Future build() async { _repo = getIt(); _exerciseRepo = getIt(); final sessions = await _repo.getAllSessions(); return AnalysisState(sessions: sessions); } Future createSession(String name, String videoPath) async { final session = await _repo.createSession(name, videoPath); final sessions = await _repo.getAllSessions(); final annotations = await _repo.getAnnotations(session.id); state = AsyncValue.data( AnalysisState( sessions: sessions, activeSession: session, annotations: annotations, ), ); } Future loadSession(String id) async { final session = await _repo.getSession(id); if (session == null) return; final annotations = await _repo.getAnnotations(id); final current = state.valueOrNull ?? const AnalysisState(); state = AsyncValue.data( current.copyWith(activeSession: session, annotations: annotations), ); } Future deleteSession(String id) async { await _repo.deleteSession(id); final sessions = await _repo.getAllSessions(); final current = state.valueOrNull ?? const AnalysisState(); state = AsyncValue.data( current.copyWith( sessions: sessions, activeSession: current.activeSession?.id == id ? null : current.activeSession, annotations: current.activeSession?.id == id ? [] : current.annotations, ), ); } Future addAnnotation({ required String name, required String description, required double startTime, required double endTime, required String color, }) async { final current = state.valueOrNull; if (current?.activeSession == null) return; await _repo.addAnnotation( sessionId: current!.activeSession!.id, name: name, description: description, startTime: startTime, endTime: endTime, color: color, ); final annotations = await _repo.getAnnotations(current.activeSession!.id); state = AsyncValue.data(current.copyWith(annotations: annotations)); } Future updateAnnotation(AnnotationEntity annotation) async { final current = state.valueOrNull; if (current?.activeSession == null) return; await _repo.updateAnnotation( id: annotation.id, name: annotation.name ?? '', description: annotation.description ?? '', color: annotation.color ?? 'grey', ); final annotations = await _repo.getAnnotations(current!.activeSession!.id); state = AsyncValue.data(current.copyWith(annotations: annotations)); } Future createExerciseFromAnnotation({ required String name, required String instructions, required String videoPath, required double startTime, required double endTime, }) async { final videoRef = '$videoPath#t=${startTime.toStringAsFixed(2)},${endTime.toStringAsFixed(2)}'; await _exerciseRepo.create( name: name, instructions: instructions.isEmpty ? null : instructions, videoUrl: videoRef, ); } Future deleteAnnotation(String id) async { await _repo.deleteAnnotation(id); final current = state.valueOrNull; if (current?.activeSession == null) return; final annotations = await _repo.getAnnotations(current!.activeSession!.id); state = AsyncValue.data(current.copyWith(annotations: annotations)); } }