import '../models/file_item.dart';
import '../models/viewer_session.dart';
import '../models/editor_session.dart';
import '../models/annotation.dart';
import '../repositories/file_repository.dart';
import '../services/file_service.dart';

class HttpFileRepository implements FileRepository {
  final FileService _fileService;
  HttpFileRepository(this._fileService);

  @override
  Future<List<FileItem>> getFiles(String orgId, String path) async {
    return await _fileService.getFiles(orgId, path);
  }

  @override
  Future<FileItem?> getFile(String orgId, String path) async {
    // Not implemented in API yet; fallback to listing
    final files = await getFiles(orgId, path);
    for (final f in files) {
      if (f.path == path) return f;
    }
    return null;
  }

  @override
  Future<void> uploadFile(String orgId, FileItem file) async {
    await _fileService.uploadFile(orgId, file);
  }

  @override
  Future<void> deleteFile(String orgId, String path) async {
    await _fileService.deleteFile(orgId, path);
  }

  @override
  Future<void> createFolder(
    String orgId,
    String parentPath,
    String folderName,
  ) async {
    await _fileService.createFolder(orgId, parentPath, folderName);
  }

  @override
  Future<void> moveFile(
    String orgId,
    String sourcePath,
    String targetPath,
  ) async {
    await _fileService.moveFile(orgId, sourcePath, targetPath);
  }

  @override
  Future<void> renameFile(String orgId, String path, String newName) async {
    throw UnimplementedError();
  }

  @override
  Future<List<FileItem>> searchFiles(String orgId, String query) async {
    // Not yet parameterized on API side; fallback to client-side filter
    final files = await getFiles(orgId, '/');
    return files
        .where((f) => f.name.toLowerCase().contains(query.toLowerCase()))
        .toList();
  }

  @override
  Future<ViewerSession> requestViewerSession(
    String orgId,
    String fileId,
  ) async {
    return await _fileService.requestViewerSession(orgId, fileId);
  }

  @override
  Future<EditorSession> requestEditorSession(
    String orgId,
    String fileId,
  ) async {
    return await _fileService.requestEditorSession(orgId, fileId);
  }

  @override
  Future<void> saveAnnotations(
    String orgId,
    String fileId,
    List<Annotation> annotations,
  ) async {
    await _fileService.saveAnnotations(orgId, fileId, annotations);
  }
}
