-- Migration: Add sync_status table
-- OrbiQuant IA Trading Platform - Data Service
-- Date: 2024-12-08
-- Purpose: Track synchronization status for market data

-- Create sync_status table
CREATE TABLE IF NOT EXISTS market_data.sync_status (
    id SERIAL PRIMARY KEY,
    ticker_id INTEGER NOT NULL REFERENCES market_data.tickers(id) ON DELETE CASCADE,
    timeframe VARCHAR(20) NOT NULL,
    last_sync_timestamp TIMESTAMP,
    last_sync_rows INTEGER DEFAULT 0,
    sync_status VARCHAR(20) NOT NULL DEFAULT 'pending',
    error_message TEXT,
    created_at TIMESTAMP DEFAULT NOW(),
    updated_at TIMESTAMP DEFAULT NOW(),

    -- Constraints
    CONSTRAINT unique_ticker_timeframe UNIQUE (ticker_id, timeframe),
    CONSTRAINT valid_status CHECK (sync_status IN ('pending', 'in_progress', 'success', 'failed', 'partial'))
);

-- Indexes for performance
CREATE INDEX IF NOT EXISTS idx_sync_status_ticker_id ON market_data.sync_status(ticker_id);
CREATE INDEX IF NOT EXISTS idx_sync_status_timeframe ON market_data.sync_status(timeframe);
CREATE INDEX IF NOT EXISTS idx_sync_status_status ON market_data.sync_status(sync_status);
CREATE INDEX IF NOT EXISTS idx_sync_status_last_sync ON market_data.sync_status(last_sync_timestamp);

-- Comments
COMMENT ON TABLE market_data.sync_status IS 'Tracks synchronization status for market data from external providers';
COMMENT ON COLUMN market_data.sync_status.ticker_id IS 'Reference to ticker being synced';
COMMENT ON COLUMN market_data.sync_status.timeframe IS 'Timeframe being synced (1min, 5min, 1hour, etc)';
COMMENT ON COLUMN market_data.sync_status.last_sync_timestamp IS 'Last successful sync timestamp';
COMMENT ON COLUMN market_data.sync_status.last_sync_rows IS 'Number of rows inserted in last sync';
COMMENT ON COLUMN market_data.sync_status.sync_status IS 'Status: pending, in_progress, success, failed, partial';
COMMENT ON COLUMN market_data.sync_status.error_message IS 'Error message if sync failed';

-- Create updated_at trigger
CREATE OR REPLACE FUNCTION update_sync_status_timestamp()
RETURNS TRIGGER AS $$
BEGIN
    NEW.updated_at = NOW();
    RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER sync_status_updated_at
    BEFORE UPDATE ON market_data.sync_status
    FOR EACH ROW
    EXECUTE FUNCTION update_sync_status_timestamp();

-- Grant permissions (adjust as needed)
GRANT SELECT, INSERT, UPDATE, DELETE ON market_data.sync_status TO orbiquant_user;
GRANT USAGE, SELECT ON SEQUENCE market_data.sync_status_id_seq TO orbiquant_user;
