-- Posts table: stores blog post information
CREATE TABLE IF NOT EXISTS posts (
    id BIGSERIAL PRIMARY KEY,
    title VARCHAR(255) NOT NULL,
    content TEXT NOT NULL,
    view_count BIGINT DEFAULT 0,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);

-- Index for sorting posts by creation date
CREATE INDEX IF NOT EXISTS idx_posts_created_at ON posts(created_at DESC);

-- Index for sorting posts by view count (popular posts)
CREATE INDEX IF NOT EXISTS idx_posts_view_count ON posts(view_count DESC);

-- Index for sorting posts by update date
CREATE INDEX IF NOT EXISTS idx_posts_updated_at ON posts(updated_at DESC);

-- Combined index for pagination queries
CREATE INDEX IF NOT EXISTS idx_posts_created_view ON posts(created_at DESC, view_count DESC);

-- Comments on columns
COMMENT ON TABLE posts IS 'Blog posts table containing article information';
COMMENT ON COLUMN posts.id IS 'Primary key, auto-incrementing post ID';
COMMENT ON COLUMN posts.title IS 'Post title';
COMMENT ON COLUMN posts.content IS 'Post content in text format';
COMMENT ON COLUMN posts.view_count IS 'Number of times the post has been viewed';
COMMENT ON COLUMN posts.created_at IS 'Timestamp when the post was created';
COMMENT ON COLUMN posts.updated_at IS 'Timestamp when the post was last updated';