import 'package:riverpod_annotation/riverpod_annotation.dart'; import 'package:trainhub_flutter/injection.dart'; import 'package:trainhub_flutter/domain/entities/program_workout.dart'; import 'package:trainhub_flutter/domain/repositories/program_repository.dart'; import 'package:trainhub_flutter/domain/repositories/training_plan_repository.dart'; import 'package:trainhub_flutter/domain/repositories/exercise_repository.dart'; import 'package:trainhub_flutter/presentation/calendar/calendar_state.dart'; part 'calendar_controller.g.dart'; @riverpod class CalendarController extends _$CalendarController { late final ProgramRepository _programRepo; late final TrainingPlanRepository _planRepo; late final ExerciseRepository _exerciseRepo; @override Future build() async { _programRepo = getIt(); _planRepo = getIt(); _exerciseRepo = getIt(); final programs = await _programRepo.getAllPrograms(); final plans = await _planRepo.getAll(); final exercises = await _exerciseRepo.getAll(); if (programs.isEmpty) { return CalendarState(plans: plans, exercises: exercises); } final activeProgram = programs.first; final weeks = await _programRepo.getWeeks(activeProgram.id); final workouts = await _programRepo.getWorkouts(activeProgram.id); return CalendarState( programs: programs, activeProgram: activeProgram, weeks: weeks, workouts: workouts, plans: plans, exercises: exercises, ); } Future loadProgram(String id) async { final program = await _programRepo.getProgram(id); if (program == null) return; final weeks = await _programRepo.getWeeks(id); final workouts = await _programRepo.getWorkouts(id); final current = state.valueOrNull ?? const CalendarState(); state = AsyncValue.data( current.copyWith( activeProgram: program, weeks: weeks, workouts: workouts, ), ); } Future createProgram(String name) async { final program = await _programRepo.createProgram(name); await _reloadFull(); await loadProgram(program.id); } Future deleteProgram(String id) async { await _programRepo.deleteProgram(id); state = await AsyncValue.guard(() => build()); } Future duplicateProgram(String sourceId) async { await _programRepo.duplicateProgram(sourceId); await _reloadFull(); } Future addWeek() async { final current = state.valueOrNull; if (current?.activeProgram == null) return; final nextPosition = current!.weeks.isEmpty ? 1 : current.weeks.last.position + 1; await _programRepo.addWeek(current.activeProgram!.id, nextPosition); await _reloadProgramDetails(); } Future deleteWeek(String id) async { await _programRepo.deleteWeek(id); await _reloadProgramDetails(); } Future updateWeekNote(String weekId, String note) async { await _programRepo.updateWeekNote(weekId, note); await _reloadProgramDetails(); } Future addWorkout(ProgramWorkoutEntity workout) async { await _programRepo.addWorkout(workout); await _reloadProgramDetails(); } Future updateWorkout(ProgramWorkoutEntity workout) async { await _programRepo.updateWorkout(workout); await _reloadProgramDetails(); } Future deleteWorkout(String id) async { await _programRepo.deleteWorkout(id); await _reloadProgramDetails(); } Future toggleWorkoutComplete(String id, bool currentStatus) async { await _programRepo.toggleWorkoutComplete(id, currentStatus); await _reloadProgramDetails(); } Future _reloadProgramDetails() async { final current = state.valueOrNull; if (current?.activeProgram == null) return; final weeks = await _programRepo.getWeeks(current!.activeProgram!.id); final workouts = await _programRepo.getWorkouts(current.activeProgram!.id); state = AsyncValue.data( current.copyWith(weeks: weeks, workouts: workouts), ); } Future _reloadFull() async { state = await AsyncValue.guard(() => build()); } }