-- Stonks Oracle - Operator approval workflow for live trading mode
-- Tracks pending, approved, rejected, and expired approval requests
-- for orders that require operator sign-off before broker submission.
-- Requirements: 8.2

CREATE TABLE operator_approvals (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    order_job JSONB NOT NULL DEFAULT '{}',
    recommendation_id UUID REFERENCES recommendations(id),
    ticker VARCHAR(20) NOT NULL,
    side VARCHAR(10) NOT NULL DEFAULT 'buy',
    quantity NUMERIC NOT NULL DEFAULT 0,
    estimated_value NUMERIC NOT NULL DEFAULT 0,
    status VARCHAR(20) NOT NULL DEFAULT 'pending',
    risk_evaluation_id UUID,
    requested_by VARCHAR(200) NOT NULL DEFAULT 'system',
    reviewed_by VARCHAR(200),
    review_note TEXT,
    expires_at TIMESTAMPTZ NOT NULL,
    requested_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    reviewed_at TIMESTAMPTZ,
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE INDEX idx_operator_approvals_status ON operator_approvals(status);
CREATE INDEX idx_operator_approvals_ticker ON operator_approvals(ticker);
CREATE INDEX idx_operator_approvals_expires ON operator_approvals(expires_at)
    WHERE status = 'pending';
