// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html

syntax = "proto3";

package anki.import_export;

import "anki/collection.proto";
import "anki/notes.proto";
import "anki/generic.proto";

service ImportExportService {
  rpc ImportCollectionPackage(ImportCollectionPackageRequest)
      returns (generic.Empty);
  rpc ExportCollectionPackage(ExportCollectionPackageRequest)
      returns (generic.Empty);
  rpc ImportAnkiPackage(ImportAnkiPackageRequest) returns (ImportResponse);
  rpc ExportAnkiPackage(ExportAnkiPackageRequest) returns (generic.UInt32);
  rpc GetCsvMetadata(CsvMetadataRequest) returns (CsvMetadata);
  rpc ImportCsv(ImportCsvRequest) returns (ImportResponse);
  rpc ImportJson(generic.String) returns (ImportResponse);
}

message ImportCollectionPackageRequest {
  string col_path = 1;
  string backup_path = 2;
  string media_folder = 3;
  string media_db = 4;
}

message ExportCollectionPackageRequest {
  string out_path = 1;
  bool include_media = 2;
  bool legacy = 3;
}

message ImportAnkiPackageRequest {
  string package_path = 1;
}

message ImportResponse {
  message Note {
    notes.NoteId id = 1;
    repeated string fields = 2;
  }
  message Log {
    repeated Note new = 1;
    repeated Note updated = 2;
    repeated Note duplicate = 3;
    repeated Note conflicting = 4;
  }
  collection.OpChanges changes = 1;
  Log log = 2;
}

message ExportAnkiPackageRequest {
  string out_path = 1;
  bool with_scheduling = 2;
  bool with_media = 3;
  bool legacy = 4;
  oneof selector {
    generic.Empty whole_collection = 5;
    int64 deck_id = 6;
    notes.NoteIds note_ids = 7;
  }
}

message PackageMetadata {
  enum Version {
    VERSION_UNKNOWN = 0;
    // When `meta` missing, and collection.anki2 file present.
    VERSION_LEGACY_1 = 1;
    // When `meta` missing, and collection.anki21 file present.
    VERSION_LEGACY_2 = 2;
    // Implies MediaEntry media map, and zstd compression.
    // collection.21b file
    VERSION_LATEST = 3;
  }

  Version version = 1;
}

message MediaEntries {
  message MediaEntry {
    string name = 1;
    uint32 size = 2;
    bytes sha1 = 3;

    /// Legacy media maps may include gaps in the media list, so the original
    /// file index is recorded when importing from a HashMap. This field is not
    /// set when exporting.
    optional uint32 legacy_zip_filename = 255;
  }

  repeated MediaEntry entries = 1;
}

message ImportCsvRequest {
  message CsvColumn {
    enum Other {
      IGNORE = 0;
      TAGS = 1;
    }
    oneof variant {
      uint32 field = 1;
      Other other = 2;
    }
  }
  string path = 1;
  int64 deck_id = 2;
  int64 notetype_id = 3;
  repeated CsvColumn columns = 4;
  uint32 delimiter = 5;
  bool is_html = 6;
}

message CsvMetadataRequest {
  string path = 1;
  optional uint32 delimiter = 2;
}

message CsvMetadata {
  uint32 delimiter = 1;
  string tags = 2;
  repeated string columns = 3;
  int64 deck_id = 4;
  int64 notetype_id = 5;
  optional bool is_html = 6;
}
