import 'package:vital_local_database/core/index.dart';
import 'package:vitalapp/database/entities/defines.dart';
import 'package:vitalapp/database/entities/diagnosis.dart';

import 'interfaces/diagnosis.dart';

class DiagnosisRepository
    extends BaseDbRepository<DiagnosisEntity, DiagnosisColumnsDefine>
    implements IDiagnosisRepository {
  DiagnosisRepository(super.database);

  @override
  DiagnosisEntity createEntityInstance() => DiagnosisEntity();

  @override
  Future<List<DiagnosisEntity>> getListByPatientCode(String patientCode,
      {OfflineDataSyncState? syncState}) async {
    final query = queryable.where((x) {
      final List<IDbColumnCondition> arr = [];
      arr.add(x.isValid.equals(true));
      arr.add(x.patientCode.equals(patientCode));
      if (syncState != null) {
        arr.add(x.syncState.equals(syncState));
      }
      return arr;
    });
    final list = await query.toList();
    return list;
  }

  @override
  Future<int> getNotUploadedCountByPatientCode(String patientCode) async {
    final query = queryable.where((x) {
      final List<IDbColumnCondition> arr = [];
      arr.add(x.isValid.equals(true));
      arr.add(x.patientCode.equals(patientCode));
      arr.add(x.syncState.notEquals(OfflineDataSyncState.success));
      return arr;
    });
    final listCount = await query.count();
    return listCount;
  }

  @override
  Future<List<DiagnosisEntity>> getNotUploadedListByPatientCode(
      String patientCode) async {
    final query = queryable.where((x) {
      final List<IDbColumnCondition> arr = [];
      arr.add(x.isValid.equals(true));
      arr.add(x.patientCode.equals(patientCode));

      arr.add(x.syncState.notEquals(OfflineDataSyncState.success));

      return arr;
    });
    final list = await query.toList();
    return list;
  }
}
