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";
import "alt/v1/market.proto";
import "alt/v1/backtest.proto";

// live_trading.proto defines the broker-neutral live trading command/query
// surface. This subtask only wires up the broker capability probe
// (GetLiveBrokerCapabilities); subsequent subtasks add order/position/audit
// message families on top of the same proto.

// OrderTimeInForce is a string-typed field in the live domain, so we use
// string here as well. The protobuf enum is unnecessary since brokers may
// return values we haven't enumerated.

// GetLiveBrokerCapabilitiesRequest is a query sent to the worker to discover
// what markets, venues, and order types the live broker adapter supports.
message GetLiveBrokerCapabilitiesRequest {
  string account_id = 1;
}

// GetLiveBrokerCapabilitiesResponse wraps the broker capability report or an
// error explaining why the probe failed.
message GetLiveBrokerCapabilitiesResponse {
  LiveBrokerCapability broker_capabilities = 1;
  ErrorInfo error = 2;
}

// LiveBrokerCapability describes what a live broker supports. It mirrors the
// domain types in packages/domain/trading but is wire-format only so that
// generated code stays minimal for this milestone.
message LiveBrokerCapability {
  string broker = 1;
  repeated Market markets = 2;
  repeated Venue venues = 3;
  repeated string order_types = 4;
  bool supports_amend = 5;
  bool supports_cancel = 6;
  bool supports_account = 7;
  bool supports_audit_tags = 8;
  int64 checked_at_unix_ms = 9;
}

// LiveOrderIntent mirrors the backtest/trading domain intent but belongs to
// the live protobuf surface so live consumers can decode it without importing
// backtest messages. Side/type/status stay string-typed to match the domain
// vocabulary and to keep the lifecycle additive without new enums.
message LiveOrderIntent {
  string instrument_id = 1;
  // side is "buy" or "sell".
  string side = 2;
  Quantity quantity = 3;
  // type is "market", "limit", or broker-specific (e.g. "kis_best_limit").
  string type = 4;
  string time_in_force = 5;
  Price limit_price = 6;
  string broker_client_id = 7;
  map<string, string> custom_tags = 8;
}

// LiveOrder is a broker-submitted order with execution status updates. It uses
// string-typed side/type/status so unknown broker values are preserved.
message LiveOrder {
  string id = 1;
  string broker_id = 2;
  string client_id = 3;
  string account_id = 4;
  string instrument_id = 5;
  string side = 6;
  Quantity quantity = 7;
  string type = 8;
  string time_in_force = 9;
  Price limit_price = 10;
  string status = 11;
  // broker_status mirrors the raw status reported by the broker.
  string broker_status = 12;
  BacktestTrade fill = 13;
  string rejection_reason = 14;
  int64 created_at_unix_ms = 15;
  int64 updated_at_unix_ms = 16;
}

// LiveAccountSnapshot carries live account state from a broker.
message LiveAccountSnapshot {
  string broker = 1;
  string account_id = 2;
  Decimal cash = 3;
  Decimal total_equity = 4;
  Decimal available_margin = 5;
  int64 snapshot_time_unix_ms = 6;
}

// LivePositionSnapshot is a single position inside a live account.
message LivePositionSnapshot {
  string instrument_id = 1;
  string side = 2;
  Quantity quantity = 3;
  Price avg_price = 4;
  Price current_price = 5;
  Decimal market_value = 6;
  Decimal pnl = 7;
}

// LiveAuditEvent is a broker-neutral event record for audit trails.
message LiveAuditEvent {
  string type = 1;
  int64 timestamp_unix_ms = 2;
  string broker = 3;
  string account_id = 4;
  map<string, string> detail = 5;
}
