import 'package:drift/drift.dart' show Value;
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

import 'package:household_keeper/core/database/database.dart';
import 'package:household_keeper/core/providers/database_provider.dart';
import 'package:household_keeper/features/tasks/domain/effort_level.dart';
import 'package:household_keeper/features/tasks/domain/frequency.dart';
import 'package:household_keeper/features/tasks/domain/task_sort_option.dart';
import 'package:household_keeper/features/tasks/presentation/sort_preference_notifier.dart';

part 'task_providers.g.dart';

/// Sort a list of [Task] by the given [sortOption].
///
/// Returns a new sorted list; never mutates the original.
List<Task> _sortTasksRaw(List<Task> tasks, TaskSortOption sortOption) {
  final sorted = List<Task>.from(tasks);
  switch (sortOption) {
    case TaskSortOption.alphabetical:
      sorted.sort((a, b) => a.name.toLowerCase().compareTo(
            b.name.toLowerCase(),
          ));
    case TaskSortOption.interval:
      sorted.sort((a, b) {
        final cmp = a.intervalType.index.compareTo(b.intervalType.index);
        if (cmp != 0) return cmp;
        return a.intervalDays.compareTo(b.intervalDays);
      });
    case TaskSortOption.effort:
      sorted.sort((a, b) => a.effortLevel.index.compareTo(b.effortLevel.index));
  }
  return sorted;
}

/// Stream provider family for tasks in a specific room, sorted by active sort preference.
///
/// Defined manually because riverpod_generator has trouble with drift's
/// generated [Task] type in family provider return types.
final tasksInRoomProvider =
    StreamProvider.family.autoDispose<List<Task>, int>((ref, roomId) {
  final db = ref.watch(appDatabaseProvider);
  final sortOption = ref.watch(sortPreferenceProvider);
  return db.tasksDao
      .watchTasksInRoom(roomId)
      .map((tasks) => _sortTasksRaw(tasks, sortOption));
});

/// Notifier for task mutations: create, update, delete, complete.
@riverpod
class TaskActions extends _$TaskActions {
  @override
  FutureOr<void> build() {}

  Future<int> createTask({
    required int roomId,
    required String name,
    String? description,
    required IntervalType intervalType,
    required int intervalDays,
    int? anchorDay,
    required EffortLevel effortLevel,
    required DateTime nextDueDate,
  }) async {
    final db = ref.read(appDatabaseProvider);
    return db.tasksDao.insertTask(TasksCompanion.insert(
      roomId: roomId,
      name: name,
      description: Value(description),
      intervalType: intervalType,
      intervalDays: Value(intervalDays),
      anchorDay: Value(anchorDay),
      effortLevel: effortLevel,
      nextDueDate: nextDueDate,
    ));
  }

  Future<void> updateTask(Task task) async {
    final db = ref.read(appDatabaseProvider);
    await db.tasksDao.updateTask(task);
  }

  Future<void> deleteTask(int taskId) async {
    final db = ref.read(appDatabaseProvider);
    await db.tasksDao.deleteTask(taskId);
  }

  Future<void> completeTask(int taskId) async {
    final db = ref.read(appDatabaseProvider);
    await db.tasksDao.completeTask(taskId);
  }

  /// Smart delete: hard-deletes tasks with no completions, soft-deletes tasks with completions.
  Future<void> smartDeleteTask(int taskId) async {
    final db = ref.read(appDatabaseProvider);
    final completionCount = await db.tasksDao.getCompletionCount(taskId);
    if (completionCount == 0) {
      await db.tasksDao.deleteTask(taskId);
    } else {
      await db.tasksDao.softDeleteTask(taskId);
    }
  }
}
