import 'package:equatable/equatable.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flux/core/blocs/session/session_cubit.dart'; import 'package:flux/features/master_data/staff/models/staff_member_model.dart'; import 'package:flux/features/settings/data/settings_repository.dart'; import 'package:flux/features/tasks/data/task_repository.dart'; import 'package:flux/features/tasks/models/task_model.dart'; import 'package:flux/features/tasks/models/task_reminder_config.dart'; import 'package:flux/features/tasks/models/task_status.dart'; import 'package:get_it/get_it.dart'; part 'task_form_state.dart'; class TaskFormCubit extends Cubit { final TaskRepository _repository = GetIt.I.get(); final SettingsRepository _settingsRepository = GetIt.I .get(); final SessionCubit _sessionCubit = GetIt.I.get(); TaskFormCubit({TaskModel? existingTask}) : super( TaskFormState( id: existingTask?.id, title: existingTask?.title ?? '', description: existingTask?.description ?? '', dueDate: existingTask?.dueDate, isGlobal: existingTask?.isGlobal ?? false, selectedStaffIds: existingTask?.assignedToIds ?? [], ), ) { if (existingTask == null) { _initializeNewTaskReminders(); } else { _loadExistingTaskReminders(existingTask.id!); } } String get _companyId => _sessionCubit.state.company!.id!; String get _currentUserId => _sessionCubit.state.currentStaffMember!.id!; // --- INIT REMINDER NUOVO TASK --- Future _initializeNewTaskReminders() async { try { final defaults = await _settingsRepository.getMyReminderDefaults( companyId: _companyId, staffId: _currentUserId, ); final initialReminders = defaults .map( (d) => TaskReminderConfig( minutesBefore: d.minutesBefore, channel: d.channel, ), ) .toList(); emit(state.copyWith(reminders: initialReminders)); } catch (e) { // Fallback in caso di errore emit( state.copyWith( reminders: const [ TaskReminderConfig(minutesBefore: 15, channel: 'push'), ], ), ); } } // --- INIT REMINDER TASK ESISTENTE --- Future _loadExistingTaskReminders(String taskId) async { try { // Recuperiamo SOLO i reminder non forzati dell'utente loggato per popolare il form final existingConfigs = await _repository.fetchPersonalReminders( taskId: taskId, staffId: _currentUserId, ); emit(state.copyWith(reminders: existingConfigs)); } catch (e) { print('Errore caricamento reminder: $e'); } } // --- UPDATE CAMPI BASE --- void updateTitle(String t) => emit(state.copyWith(title: t)); void updateDescription(String d) => emit(state.copyWith(description: d)); void updateDueDate(DateTime? d) => emit(state.copyWith(dueDate: d)); void toggleGlobalScope(bool g) => emit(state.copyWith(isGlobal: g)); void toggleStaffSelection(String staffId) { final updated = List.from(state.selectedStaffIds); updated.contains(staffId) ? updated.remove(staffId) : updated.add(staffId); emit(state.copyWith(selectedStaffIds: updated)); } // --- GESTIONE REMINDER NEL FORM --- void addReminderRule(int minutesBefore, String channel) { final updated = List.from(state.reminders); final newConfig = TaskReminderConfig( minutesBefore: minutesBefore, channel: channel, ); if (!updated.contains(newConfig)) { updated.add(newConfig); updated.sort((a, b) => a.minutesBefore.compareTo(b.minutesBefore)); emit(state.copyWith(reminders: updated)); } } void removeReminderRule(int index) { final updated = List.from(state.reminders) ..removeAt(index); emit(state.copyWith(reminders: updated)); } // --- SALVATAGGIO FINALE --- Future saveTask() async { if (!state.isFormValid) return; emit(state.copyWith(status: TaskFormStatus.submitting)); final taskToSave = TaskModel( id: state.id, companyId: _companyId, createdBy: _currentUserId, title: state.title.trim(), description: state.description.trim(), dueDate: state.dueDate, isGlobal: state.isGlobal, assignedToIds: state.selectedStaffIds, ); try { if (state.id == null) { // NUOVO TASK -> CREATE // Qui potresti passare un managerForcedOverride se implementi la UI per quello await _repository.createTask( task: taskToSave, assignedStaffIds: state.selectedStaffIds, currentUserId: _currentUserId, currentUserCustomReminders: state.reminders, ); } else { // VECCHIO TASK -> UPDATE await _repository.updateTask( task: taskToSave, assignedStaffIds: state.selectedStaffIds, currentUserId: _currentUserId, currentUserCustomReminders: state.reminders, ); } emit(state.copyWith(status: TaskFormStatus.success)); } catch (e) { emit( state.copyWith( status: TaskFormStatus.failure, errorMessage: e.toString(), ), ); } } }