import 'package:drift/drift.dart';
import 'package:im_app/data/local/drift/app_database.dart';
import 'package:im_app/domain/entities/user_request_history.dart';
import 'package:im_app/domain/repositories/user_request_history_repository.dart';
import 'package:storage_sdk/storage_sdk.dart';

class UserRequestHistoryRepositoryImpl implements UserRequestHistoryRepository {
  final StorageSdkApi _storage;

  UserRequestHistoryRepositoryImpl(this._storage);

  // ── DB row → Domain ──────────────────────────────────────────────────────

  UserRequestHistory _toEntity(DriftUserRequestHistory row) =>
      UserRequestHistory(
        id: row.id,
        status: row.status,
        createdAt: row.createdAt,
      );

  // ── Domain → DB companion (internal only) ────────────────────────────────

  UserRequestHistoriesCompanion _toCompanion(UserRequestHistory history) =>
      UserRequestHistoriesCompanion(
        id: Value(history.id),
        status: Value(history.status),
        createdAt: Value(history.createdAt),
      );

  // ── 监听 ─────────────────────────────────────────────────────────────────

  @override
  Stream<List<UserRequestHistory>> watchAll() {
    return _storage.watchAll<DriftUserRequestHistory>().map(
      (rows) => rows.map(_toEntity).toList(),
    );
  }

  @override
  Stream<UserRequestHistory?> watchById(int id) {
    return _storage
        .watchFirst<DriftUserRequestHistory, $UserRequestHistoriesTable>(
          (t) => t.id.equals(id),
        )
        .map((row) => row != null ? _toEntity(row) : null);
  }

  @override
  Stream<List<UserRequestHistory>> watchByStatus(int status) {
    return _storage
        .watchWhere<DriftUserRequestHistory, $UserRequestHistoriesTable>(
          (t) => t.status.equals(status),
        )
        .map((rows) => rows.map(_toEntity).toList());
  }

  // ── 读取 ─────────────────────────────────────────────────────────────────

  @override
  Future<List<UserRequestHistory>> getAll() async {
    final rows = await _storage.selectAll<DriftUserRequestHistory>();
    return rows.map(_toEntity).toList();
  }

  @override
  Future<UserRequestHistory?> getById(int id) async {
    final row = await _storage
        .selectFirst<DriftUserRequestHistory, $UserRequestHistoriesTable>(
          (t) => t.id.equals(id),
        );
    return row != null ? _toEntity(row) : null;
  }

  @override
  Future<List<UserRequestHistory>> getByStatus(int status) async {
    final rows = await _storage
        .selectWhere<DriftUserRequestHistory, $UserRequestHistoriesTable>(
          (t) => t.status.equals(status),
        );
    return rows.map(_toEntity).toList();
  }

  // ── 写入 ─────────────────────────────────────────────────────────────────

  @override
  Future<void> insertOrReplace(UserRequestHistory history) async {
    await _storage.insertOrReplace<DriftUserRequestHistory>(
      _toCompanion(history),
    );
  }

  @override
  Future<void> insertOrReplaceAll(List<UserRequestHistory> histories) async {
    await _storage.batchInsertOrReplace<DriftUserRequestHistory>(
      histories.map(_toCompanion).toList(),
    );
  }

  @override
  Future<void> update(UserRequestHistory history) async {
    await _storage
        .updateWhere<DriftUserRequestHistory, $UserRequestHistoriesTable>(
          _toCompanion(history),
          (t) => t.id.equals(history.id),
        );
  }

  // ── 删除 ─────────────────────────────────────────────────────────────────

  @override
  Future<void> delete(int id) async {
    await _storage
        .deleteWhere<DriftUserRequestHistory, $UserRequestHistoriesTable>(
          (t) => t.id.equals(id),
        );
  }

  @override
  Future<void> clear() async {
    await _storage.deleteAll<DriftUserRequestHistory>();
  }
}
