package com.dexorder.flink.publisher;

import java.io.Serializable;
import java.util.List;

/**
 * Wrapper for OHLCBatch protobuf message.
 * Contains metadata and OHLC rows from ingestor.
 */
public class OHLCBatchWrapper implements Serializable {
    private static final long serialVersionUID = 1L;

    private final String requestId;
    private final String clientId;
    private final String ticker;
    private final int periodSeconds;
    private final long startTime;
    private final long endTime;
    private final String status; // OK, NOT_FOUND, ERROR
    private final String errorMessage;
    private final List<OHLCRow> rows;

    public OHLCBatchWrapper(
            String requestId,
            String clientId,
            String ticker,
            int periodSeconds,
            long startTime,
            long endTime,
            String status,
            String errorMessage,
            List<OHLCRow> rows
    ) {
        this.requestId = requestId;
        this.clientId = clientId;
        this.ticker = ticker;
        this.periodSeconds = periodSeconds;
        this.startTime = startTime;
        this.endTime = endTime;
        this.status = status;
        this.errorMessage = errorMessage;
        this.rows = rows;
    }

    public String getRequestId() {
        return requestId;
    }

    public String getClientId() {
        return clientId;
    }

    public String getTicker() {
        return ticker;
    }

    public int getPeriodSeconds() {
        return periodSeconds;
    }

    public long getStartTime() {
        return startTime;
    }

    public long getEndTime() {
        return endTime;
    }

    public String getStatus() {
        return status;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public List<OHLCRow> getRows() {
        return rows;
    }

    public int getRowCount() {
        return rows != null ? rows.size() : 0;
    }

    public boolean hasError() {
        return "ERROR".equals(status);
    }

    public boolean isNotFound() {
        return "NOT_FOUND".equals(status);
    }

    public boolean isOk() {
        return "OK".equals(status);
    }

    @Override
    public String toString() {
        return "OHLCBatchWrapper{" +
                "requestId='" + requestId + '\'' +
                ", clientId='" + clientId + '\'' +
                ", ticker='" + ticker + '\'' +
                ", periodSeconds=" + periodSeconds +
                ", status='" + status + '\'' +
                ", rowCount=" + getRowCount() +
                '}';
    }

    /**
     * Single OHLC row
     */
    public static class OHLCRow implements Serializable {
        private static final long serialVersionUID = 1L;

        private final long timestamp;
        private final String ticker;
        private final long open;
        private final long high;
        private final long low;
        private final long close;
        private final long volume;

        public OHLCRow(long timestamp, String ticker, long open, long high,
                      long low, long close, long volume) {
            this.timestamp = timestamp;
            this.ticker = ticker;
            this.open = open;
            this.high = high;
            this.low = low;
            this.close = close;
            this.volume = volume;
        }

        public long getTimestamp() {
            return timestamp;
        }

        public String getTicker() {
            return ticker;
        }

        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;
        }

        @Override
        public String toString() {
            return "OHLCRow{" +
                    "timestamp=" + timestamp +
                    ", ticker='" + ticker + '\'' +
                    ", open=" + open +
                    ", high=" + high +
                    ", low=" + low +
                    ", close=" + close +
                    ", volume=" + volume +
                    '}';
        }
    }
}
