import 'package:drift/drift.dart';
import 'package:sendtrain/database/database.dart';

part 'session_activities_dao.g.dart';

@DriftAccessor(tables: [SessionActivities])
class SessionActivitiesDao extends DatabaseAccessor<AppDatabase>
    with _$SessionActivitiesDaoMixin {
  SessionActivitiesDao(super.db);

  Future createOrUpdate(SessionActivitiesCompanion sessionActivity) =>
      into(sessionActivities).insertOnConflictUpdate(sessionActivity);

  Future<List<SessionActivity>> all() async {
    return await select(sessionActivities).get();
  }

  Future<SessionActivity> find(int id) async {
    return await (select(sessionActivities)
          ..where((sessionActivity) => sessionActivity.id.equals(id)))
        .getSingle();
  }

  Future<List<SessionActivity>> fromSessionId(int id) async {
    final result = db.managers.sessionActivities
        .filter((sessionActivity) => sessionActivity.sessionId.id(id));

    return result.get();
  }

  Future remove(SessionActivity sessionActivity) =>
      delete(sessionActivities).delete(sessionActivity);

  Future removeAssociation(int activityId, int sessionId) {
    return (delete(sessionActivities)
          ..where((t) =>
              t.sessionId.equals(sessionId) & t.activityId.equals(activityId)))
        .go();
  }
}

// return (delete(todos)..where((t) => t.id.isSmallerThanValue(10))).go();