syntax = "proto3";

package wix.inventory.api.v1;

import "google/api/annotations.proto";
import "wix/api/annotations.proto";
import "wix/api/validations.proto";
import "wix/api/entity.proto";
import "wix/inventory/api/v1/inventoryItem.proto";
import "wix/api/callback.proto";
import "wix/api/permissions.proto";
import "wix/inventory/api/v1/query.proto";
import "google/protobuf/struct.proto";
import "google/protobuf/wrappers.proto";
import "wix/sdk/definitions.proto";
import "com/wix/ecommerce/inventory/api/v1/inventoryEvents.proto";


option java_package = "com.wix.ecommerce.inventory.api.v1";

service InventoryReadApi {
    option (wix.api.service_exposure) = PUBLIC;
    option (wix.api.service_maturity) = GA;
    option (wix.api.service_entity).message = "wix.inventory.api.v1.InventoryItemV2";

    rpc GetInventoryVariants (GetInventoryVariantsRequest) returns (GetInventoryVariantsResponse) {
        option (google.api.http) = {
            post: "/v2/inventoryItems/{inventory_id}/getVariants"
            additional_bindings {
                post: "/v2/inventoryItems/external/{external_id}/getVariants"
            }
            additional_bindings {
                post: "/v2/inventoryItems/product/{product_id}/getVariants"
            }
        };
        option (wix.api.permission) = {
            name: "WIX_STORES.READ_INVENTORY",
            type: MANUAL
        };
    }

    rpc GetInventoryItems (GetInventoryItemsRequest) returns (GetInventoryItemsResponse) {
        option (wix.api.exposure) = PRIVATE;
        option (wix.api.permission) = {
            name: "WIX_STORES.READ_INVENTORY",
            type: MANUAL
        };
    }

    // Returns a list of up inventory items, given the provided paging, sorting and filtering.
    //See [Stores Pagination](https://dev.wix.com/api/rest/wix-stores/pagination) for more information.
    rpc QueryInventory (QueryInventoryRequest) returns (QueryInventoryResponse) {
        option (google.api.http) = {
            post: "/v2/inventoryItems/query"
        };
        option (wix.api.permission) = {
            name: "WIX_STORES.READ_INVENTORY",
            type: MANUAL
        };
    }
    // Triggered when an inventory item is changed.
    option (wix.api.event) = {
       topic: "wixStores-inventory-item-changed"
       payload: "com.wix.ecommerce.inventory.api.v1.InventoryItemChanged"
       permission: "WIX_STORES.READ_INVENTORY"
    };
    // Triggered when inventory variants are changed.
    option (wix.api.event) = {
       topic: "wixStores-inventory-variant-changed"
       payload: "com.wix.ecommerce.inventory.api.v1.InventoryVariantsChanged"
       permission: "WIX_STORES.READ_INVENTORY"
    };
}

service InventoryWriteApi {
    option (wix.api.service_exposure) = PUBLIC;
    option (wix.api.service_maturity) = GA;
    option (wix.api.service_entity).message = "wix.inventory.api.v1.InventoryItemV2";

    //Updates product inventory, including total quantity, whether the product is in stock, and whether the product inventory is tracked.
    rpc UpdateInventoryVariants (UpdateInventoryVariantsRequest) returns (UpdateInventoryVariantsResponse) {
        option (wix.api.required) = "UpdateInventoryVariantsRequest.inventory_item";
        option (google.api.http) = {
            patch: "/v2/inventoryItems/product/{inventoryItem.product_id}"
            additional_bindings {
                patch: "/v2/inventoryItems/{inventoryItem.id}"
            }
            additional_bindings {
                patch: "/v2/inventoryItems/external/{inventoryItem.external_id}"
            }
        };
        option (wix.api.permission).name = "WIX_STORES.MODIFY_INVENTORY";
    }

    //Updates variants in bulk by filter, including total quantity, whether the product is in stock, and whether the product inventory is tracked.
    rpc BulkUpdateInventoryVariants (BulkUpdateInventoryVariantsRequest) returns (BulkUpdateInventoryVariantsResponse) {
        option (wix.api.required) = "BulkUpdateInventoryVariantsRequest.filter";
        option (google.api.http) = {
            patch: "/v2/inventoryItems/variants/bulk-update"
        };
        option (wix.api.maturity) = ALPHA;
        option (wix.api.exposure) = INTERNAL;
        option (wix.api.permission).name = "WIX_STORES.MODIFY_INVENTORY";
    }

    //Updates inventory tracking method in bulk by filter.
    rpc BulkUpdateInventoryItems (BulkUpdateInventoryItemsRequest) returns (BulkUpdateInventoryItemsResponse) {
        option (wix.api.required) = "BulkUpdateInventoryItemsRequest.variants_filter";
        option (wix.api.required) = "BulkUpdateInventoryItemsRequest.track_inventory";
        option (google.api.http) = {
            patch: "/v2/inventoryItems/bulk-update"
        };
        option (wix.api.maturity) = ALPHA;
        option (wix.api.exposure) = INTERNAL;
        option (wix.api.permission).name = "WIX_STORES.MODIFY_INVENTORY";
    }

    //Subtracts a set number of items from inventory.
    rpc DecrementInventory (DecrementInventoryRequest) returns (DecrementInventoryResponse) {
        option (google.api.http) = {
            post: "/v2/inventoryItems/decrement"
        };
        option (wix.api.permission).name = "WIX_STORES.MODIFY_INVENTORY";
        option (.wix.sdk.signature) = {params: "decrement_data"};
    }
    //Adds a set number of items to inventory.
    rpc IncrementInventory (IncrementInventoryRequest) returns (IncrementInventoryResponse) {
        option (google.api.http) = {
            post: "/v2/inventoryItems/increment"
        };
        option (wix.api.permission).name = "WIX_STORES.MODIFY_INVENTORY";
        option (.wix.sdk.signature) = {params: "increment_data"};

    }
}

message GetInventoryVariantsRequest {
    oneof id {
        // Inventory item ID.
        string inventory_id = 1;
        // Deprecated (use productID instead).
        string external_id = 2 [deprecated = true, (.wix.api.field_deprecated) = {
            replaced_by: "product_id"
            target_removal_date: "2024-12-31"
        }];
        // Product ID.
        string product_id = 4;
    }

    // Variant IDs to query for this inventory item (optional).
    repeated string variant_ids = 3;
}

message GetInventoryVariantsResponse {
    // Inventory item.
    InventoryItemV2 inventory_item = 1;
}

message GetInventoryItemsRequest {
    // Product IDs
    repeated string product_ids = 1;
}

message GetInventoryItemsResponse {
    // Inventory items.
    repeated InventoryItemV2 inventory_items = 1;
}

message UpdateInventoryVariantsRequest {
    // Inventory item.
    InventoryItemV2 inventory_item = 1;
    .wix.inventory.api.v1.ReasonTypeWrapper.ReasonType reason = 3 [(.wix.api.field_exposure) = PRIVATE];
}

message UpdateInventoryVariantsResponse {
}

message BulkUpdateInventoryVariantsRequest {
    // Variants filter. Learn more about [API query language](https://dev.wix.com/docs/rest/articles/getting-started/api-query-language).
    google.protobuf.Struct filter = 1;
    oneof action {
        // Change availability.
        google.protobuf.BoolValue set_in_stock = 2;
        // Set new quantity.
        google.protobuf.Int32Value set_quantity = 3;
        // Number to increment inventory by.
        google.protobuf.Int32Value increment_by = 4 [(wix.api.min) = 1];
        // Number to decrement inventory by.
        google.protobuf.Int32Value decrement_by = 5 [(wix.api.min) = 1];
    }
    .wix.inventory.api.v1.ReasonTypeWrapper.ReasonType reason = 6 [(.wix.api.field_exposure) = PRIVATE];
    bool changeTrackingMethod = 7 [(.wix.api.field_exposure) = PRIVATE];
}

message BulkUpdateInventoryVariantsResponse {
}

message BulkUpdateInventoryItemsRequest {
    // Variants filter
    google.protobuf.Struct variants_filter = 1;
    // Whether inventory is being tracked.
    google.protobuf.BoolValue track_inventory = 2;
}

message BulkUpdateInventoryItemsResponse {
}

message QueryInventoryRequest {
    Query query = 1;
}

message QueryInventoryResponse {
    //Inventory items.
    repeated InventoryItemV2 inventory_items = 1;
    //Display metadata.
    PagingMetadata metadata = 2;
    //Number of total results.
    int32 totalResults = 3;
}

message DecrementInventoryRequest {
    repeated DecrementData decrement_data = 1 [(wix.api.minSize) = 1, (wix.api.maxSize) = 300];
    .wix.inventory.api.v1.ReasonTypeWrapper.ReasonType reason = 3 [(.wix.api.field_exposure) = PRIVATE];
    bool restrict_inventory_value = 4 [(.wix.api.field_exposure) = PRIVATE];
}

message DecrementInventoryResponse {
}

message IncrementInventoryRequest {
    repeated IncrementData increment_data = 1 [(wix.api.minSize) = 1, (wix.api.maxSize) = 300];
    .wix.inventory.api.v1.ReasonTypeWrapper.ReasonType reason = 3 [(.wix.api.field_exposure) = PRIVATE];
}

message IncrementInventoryResponse {
}
