CREATE TABLE omset_tracker (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    period_type VARCHAR(20) NOT NULL,
    period_start DATE NOT NULL,
    period_end DATE NOT NULL,
    total BIGINT DEFAULT 0 NOT NULL,
    game_id UUID,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    
    CONSTRAINT fk_omset_tracker_game
        FOREIGN KEY (game_id)
        REFERENCES games(id)
        ON DELETE SET NULL,
    
    CONSTRAINT chk_omset_tracker_period_type_valid
        CHECK (period_type IN ('DAILY', 'WEEKLY', 'MONTHLY', 'TOTAL')),
    
    CONSTRAINT chk_omset_tracker_total_non_negative
        CHECK (total >= 0),
    
    CONSTRAINT chk_omset_tracker_period_valid
        CHECK (period_end >= period_start)
);

-- Create indexes
CREATE INDEX idx_omset_tracker_period_type ON omset_tracker(period_type);
CREATE INDEX idx_omset_tracker_period_start ON omset_tracker(period_start);
CREATE INDEX idx_omset_tracker_game_id ON omset_tracker(game_id);
CREATE INDEX idx_omset_tracker_created_at ON omset_tracker(created_at);
CREATE INDEX idx_omset_tracker_period_type_start ON omset_tracker(period_type, period_start);
CREATE INDEX idx_omset_tracker_game_period ON omset_tracker(game_id, period_type, period_start);
