syntax = "proto3";
import "google/protobuf/timestamp.proto";

package subs;
option go_package = "chronorpc";

service Chronobreak {
    rpc PreviewTenure(PreviewTenureRequest) returns (PreviewTenureResponse);
    rpc ReadTenure(ReadTenureRequest) returns (ReadTenureResponse);
    rpc ReadTenures(ReadTenuresRequest) returns (ReadTenuresResponse);
    rpc UpsertGrant(UpsertGrantRequest) returns (UpsertGrantResponse);
    rpc ReadGrants(ReadGrantsRequest) returns (ReadGrantsResponse);
    rpc DeleteOverrideGrant(DeleteOverrideGrantRequest) returns (DeleteOverrideGrantResponse);
}

enum TenureMethod {
    CUMULATIVE = 0;
    STREAK = 1;
}

enum Action {
    NONE = 0;
    CREATED = 1;
    CANCELED = 2;
    BACKFILLED = 3;
    GRANDFATHERED = 4;
}

message PreviewTenureRequest {
    string owner_id = 1;
    string item_id = 2;
    string domain = 3;
    google.protobuf.Timestamp start = 4;
    google.protobuf.Timestamp end = 5;
    google.protobuf.Timestamp tenure_date = 6;
}

message PreviewTenureResponse {
    string owner_id = 1;
    string item_id = 2;
    string domain = 3;
    int32 streak_value = 4;
    int32 tenure_value = 5;
}

message ReadTenureRequest {
    // owner_id is the owner of this particular tenure (usually an user id)
    string owner_id = 1;
    // item_id can be a product id or channel id
    string item_id = 2;
    // domain for which the tenure should be calculated (e.g., chan sub)
    string domain = 3;
    // calculation method for this tenure request, CUMULATIVE or STREAK
    TenureMethod tenure_method = 4;
    // date in which the tenure calculation should calculate to, serving as an as of date
    google.protobuf.Timestamp tenure_date = 5;
}

message ReadTenureResponse {
    // the owner of this particular tenure (usually an user id)
    string owner_id = 1;
    // the id of the item this tenure corresponds to (usually product or channel id)
    string item_id = 2;
    // the domain this tenure corresponds to(e.g., chan sub)
    string domain = 3;
    int32 tenure_value = 4;
    // calculation method that was used to calculate this tenure
    TenureMethod tenure_method = 5;
    // start and end date of this tenure
    google.protobuf.Timestamp start = 6;
    google.protobuf.Timestamp end = 7;
    // number of days elapsed in the current tenure month
    int32 elapsedDays = 8;
    // number of days remaining for next tenure month
    int32 daysRemaining = 9;
}

message ReadTenuresRequest {
    // owner_id is the owner of this particular tenure (usually an user id)
    string owner_id = 1;
    // item_id can be a product id or channel id
    string item_id = 2;
    // domain for which the tenure should be calculated (e.g., chan sub)
    string domain = 3;
    // calculation method for this tenure request, CUMULATIVE or STREAK
    TenureMethod tenure_method = 4;
}
message ReadTenuresResponse {
    // list of user tenures
    repeated Tenure tenures = 1;
}
message Tenure {
    string id = 1;
    // the owner of this particular tenure (usually an user id)
    string owner_id = 2;
    // the id of the item this tenure corresponds to (usually product or channel id)
    string item_id = 3;
    // the domain this tenure corresponds to(e.g., chan sub)
    string domain = 4;
    // calculation method that was used to calculate this tenure
    TenureMethod method = 5;
    // Tenure value in months
    int32 value = 6;
    Action action = 7;
    // start and end date of this tenure
    google.protobuf.Timestamp start = 8;
    google.protobuf.Timestamp end = 9;
}

message UpsertGrantRequest {
    // owner_id is usually a user id that owns the particular grant.
    string owner_id = 1;
    // item_id can be a product id or channel id.
    string item_id = 2;
    // origin_id is a unique constraint that allows for upserts. Origins are globally
    // unique and usually reference some sort of event that initiated the grant (such
    // as a purchase event). The origin id can be used to upsert or cancel a grant.
    string origin_id = 3;
    // domain groups grants for a particular use case (e.g., chan sub)
    string domain = 4;
    // start and end are ranges when the grant takes effect. These can be past/future dates.
    google.protobuf.Timestamp start = 5;
    google.protobuf.Timestamp end = 6;
    string action = 7;
}

message UpsertGrantResponse {}

message ReadGrantsRequest {
    // owner_id is usually a user id that owns the particular grant.
    string owner_id = 1;
    // item_id can be a product id or channel id.
    string item_id = 2;
    // domain groups grants for a particular use case (e.g., chan sub)
    string domain = 4;
}

message ReadGrantsResponse {
    // list of user grants
    repeated Grant grants = 1;
}

message Grant {
    string id = 1;
    // owner_id is usually a user id that owns the particular grant.
    string owner_id = 2;
    // domain groups grants for a particular use case (e.g., chan sub)
    string domain = 3;
    // item_id can be a product id or channel id.
    string item_id = 4;
    // origin is a unique constraint that allows for upserts. Origins are globally
    // unique and usually reference some sort of event that initiated the grant (such
    // as a purchase event).
    string origin = 5;
    string action = 6;
    // start and end are ranges when the grant takes effect. These can be past/future dates.
    google.protobuf.Timestamp start = 7;
    google.protobuf.Timestamp end = 8;
    google.protobuf.Timestamp updated_at = 9;
    google.protobuf.Timestamp created_at = 10;
}

message DeleteOverrideGrantRequest {
    // owner_id is usually a user id that owns the particular grant.
    string owner_id = 1;
    // item_id can be a product id or channel id.
    string item_id = 2;
    // domain groups grants for a particular use case (e.g., chan sub)
    string domain = 3;
    // unique id used to identify a grant
    string origin_id = 4;
}

message DeleteOverrideGrantResponse {}
