package com.dexorder.flink.publisher;

import java.io.Serializable;

/**
 * A single completed OHLC bar for a given ticker and period.
 * Output type of RealtimeBarFunction, input type of RealtimeBarPublisher.
 */
public class RealtimeBar implements Serializable {
    private static final long serialVersionUID = 1L;

    private String ticker;
    /** Period in seconds (e.g., 60, 300, 3600) */
    private int periodSeconds;
    /** Window start timestamp in milliseconds since epoch */
    private long windowStartMs;
    /** Scaled integer price values (same precision as source Tick) */
    private long open;
    private long high;
    private long low;
    private long close;
    /** Summed base amount across ticks in this window */
    private long volume;
    /** Number of ticks in this window */
    private int tickCount;

    public RealtimeBar() {}

    public RealtimeBar(String ticker, int periodSeconds, long windowStartMs,
                       long open, long high, long low, long close, long volume, int tickCount) {
        this.ticker = ticker;
        this.periodSeconds = periodSeconds;
        this.windowStartMs = windowStartMs;
        this.open = open;
        this.high = high;
        this.low = low;
        this.close = close;
        this.volume = volume;
        this.tickCount = tickCount;
    }

    public String getTicker() { return ticker; }
    public int getPeriodSeconds() { return periodSeconds; }
    public long getWindowStartMs() { return windowStartMs; }
    public long getOpen() { return open; }
    public long getHigh() { return high; }
    public long getLow() { return low; }
    public long getClose() { return close; }
    public long getVolume() { return volume; }
    public int getTickCount() { return tickCount; }

    public void setTicker(String ticker) { this.ticker = ticker; }
    public void setPeriodSeconds(int periodSeconds) { this.periodSeconds = periodSeconds; }
    public void setWindowStartMs(long windowStartMs) { this.windowStartMs = windowStartMs; }
    public void setOpen(long open) { this.open = open; }
    public void setHigh(long high) { this.high = high; }
    public void setLow(long low) { this.low = low; }
    public void setClose(long close) { this.close = close; }
    public void setVolume(long volume) { this.volume = volume; }
    public void setTickCount(int tickCount) { this.tickCount = tickCount; }

    /** ZMQ topic for this bar: e.g., "BTC/USDT.BINANCE|ohlc:60" */
    public String topic() {
        return ticker + "|ohlc:" + periodSeconds;
    }

    @Override
    public String toString() {
        return "RealtimeBar{ticker='" + ticker + "', period=" + periodSeconds +
                "s, windowStart=" + windowStartMs + ", O=" + open + " H=" + high +
                " L=" + low + " C=" + close + ", ticks=" + tickCount + '}';
    }
}
