syntax = "proto3";

package alt.v1;

option go_package = "git.toki-labs.com/toki/alt/packages/contracts/gen/go/alt/v1;altv1";

import "alt/v1/common.proto";

message Instrument {
  string id = 1;
  Market market = 2;
  Venue venue = 3;
  string symbol = 4;
  string name = 5;
  Currency currency = 6;
  map<string, string> provider_symbols = 7;
  AssetType asset_type = 8;
}

message Price {
  Currency currency = 1;
  Decimal amount = 2;
}

message Quantity {
  Decimal amount = 1;
}

message Bar {
  string instrument_id = 1;
  Timeframe timeframe = 2;
  int64 timestamp_unix_ms = 3;
  Price open = 4;
  Price high = 5;
  Price low = 6;
  Price close = 7;
  Quantity volume = 8;
}

message ListInstrumentsRequest {
  Market market = 1;
  string provider = 2;
}

message ListInstrumentsResponse {
  repeated Instrument instruments = 1;
  ErrorInfo error = 2;
}

message ListBarsRequest {
  string instrument_id = 1;
  Timeframe timeframe = 2;
  int64 from_unix_ms = 3;
  int64 to_unix_ms = 4;
}

message ListBarsResponse {
  repeated Bar bars = 1;
  ErrorInfo error = 2;
}

message ImportDailyBarsRequest {
  string provider = 1;
  string selector_kind = 2;
  Market market = 3;
  Venue venue = 4;
  string name = 5;
  repeated string symbols = 6;
  string from_yyyymmdd = 7;
  string to_yyyymmdd = 8;
  Timeframe timeframe = 9;
}

message ImportDailyBarsResponse {
  string provider = 1;
  int32 instrument_count = 2;
  int32 bar_count = 3;
  ErrorInfo error = 4;
}

// AggregateMonthlyBarsRequest asks the worker to aggregate stored daily bars
// into monthly bars for the given instrument scope and date range.
message AggregateMonthlyBarsRequest {
  string provider = 1;
  string selector_kind = 2;
  Market market = 3;
  Venue venue = 4;
  string name = 5;
  repeated string symbols = 6;
  string from_yyyymmdd = 7;
  string to_yyyymmdd = 8;
}

// AggregateMonthlyBarsResponse carries the result of a monthly aggregation
// operation together with provenance information for every produced monthly bar.
message AggregateMonthlyBarsResponse {
  string provider = 1;
  int32 instrument_count = 2;
  int32 source_daily_bar_count = 3;
  int32 monthly_bar_count = 4;
  repeated MonthlyProvenance provenance = 5;
  ErrorInfo error = 6;
}

// MonthlyProvenance records how a set of monthly bars was derived from daily bars.
// Each entry corresponds to one instrument's monthly bars.
message MonthlyProvenance {
  string instrument_id = 1;
  int32 source_daily_bar_count = 2;
  int32 monthly_bar_count = 3;
  string source_start_yyyymmdd = 4;
  string source_end_yyyymmdd = 5;
  string aggregation_rule_id = 6;
  Timeframe source_timeframe = 7;
  Timeframe target_timeframe = 8;
}

// SchedulerRefreshStatusRequest queries the runtime refresh status of one or
// all scheduler schedules. An empty schedule_name returns all schedules.
message SchedulerRefreshStatusRequest {
  string schedule_name = 1;
}

// SchedulerRefreshStatusEntry carries the S04 evidence fields for one schedule.
message SchedulerRefreshStatusEntry {
  string schedule = 1;
  string status = 2;             // "success", "stale", "error"
  int64 last_success_unix_ms = 3;
  string last_error = 4;
  int64 next_run_unix_ms = 5;
  int32 imported_bar_count = 6;
  int32 missing_count = 7;
  int32 gap_count = 8;
  int32 duplicate_count = 9;
  int32 provider_delay_days = 10;
}

// SchedulerRefreshStatusResponse returns the refresh status of the requested
// schedule(s). An empty entries list means no schedule matched the request.
message SchedulerRefreshStatusResponse {
  repeated SchedulerRefreshStatusEntry entries = 1;
  ErrorInfo error = 2;
}
