import 'package:vital_local_database/index.dart';
import 'package:vitalapp/database/entities/patient.dart';

import 'repositories/interfaces/patient.dart';
import 'repositories/patient.dart';

/// vital 本地数据库对象
final db = VitalDatabaseAccessor();

/// vital本地数据库访问器
class VitalDatabaseAccessor
    extends BaseDbAccessor<VitalDatabaseRepositoryStore> {
  final _store = VitalDatabaseRepositoryStore();
  final _connection = SqliteConnection(path: "vital.db");
  late final IDatabase _database;

  @override
  IDatabase get database => _database;

  @override
  VitalDatabaseRepositoryStore get repositories => _store;

  /// 初始化
  Future<void> init() async {
    _database = await _connection.open();
    await _checkTables();
    _store.regRepository<IPatientRepository>(PatientRepository(database));
  }

  Future<void> _checkTables() async {
    // await database.execute("DROP TABLE IF EXISTS Patients;");
    await database.execute(PatientEntity.TABLE_CREATE_SQL);
  }

  void dispose() {
    _connection.close();
  }
}

class VitalDatabaseRepositoryStore extends BaseDbAccessorRepositoryStore {
  /// 病人仓储
  IPatientRepository get patient => findRepository<IPatientRepository>()!;
}
