import 'package:flutter_riverpod/flutter_riverpod.dart'; import '../models/programmer_task_work_log.model.dart'; import '../utils/programmer_daily_time.dart'; import 'programmer_day_sheets_provider.dart'; import 'supabase_provider.dart'; /// Work-log entries for a task (newest first), streamed live. final programmerTaskWorkLogsProvider = StreamProvider.family, String>((ref, taskId) { final client = ref.watch(supabaseClientProvider); return client .from('programmer_task_work_logs') .stream(primaryKey: ['id']) .eq('task_id', taskId) .order('created_at', ascending: false) .map((rows) => rows.map(ProgrammerTaskWorkLog.fromMap).toList()); }); final programmerTaskWorkLogsControllerProvider = Provider((ref) { return ProgrammerTaskWorkLogsController(ref.watch(supabaseClientProvider)); }); class ProgrammerTaskWorkLogsController { ProgrammerTaskWorkLogsController(this._client); final dynamic _client; /// Adds a work-log entry. [minutes] is null for the assignee's progress notes /// (their time comes from the running clock) and set for a helper's /// Time Consumed. [workDate] overrides the date the entry is attributed to; /// when null the DB default applies (today's Manila date). Future addWorkLog({ required String taskId, required String description, int? minutes, DateTime? workDate, }) async { final userId = _client.auth.currentUser?.id; if (userId == null) throw Exception('Not authenticated'); try { await _client.from('programmer_task_work_logs').insert({ 'task_id': taskId, 'author_id': userId, 'description': description, 'minutes': ?minutes, if (workDate != null) 'work_date': formatDayKey(workDate), }); } catch (e) { if (e is DaySheetActionException) rethrow; throw DaySheetActionException(mapDaySheetError(e)); } } }