-- Add updated_at column to tablos table
ALTER TABLE tablos 
ADD COLUMN IF NOT EXISTS updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP;

-- Update existing rows to have updated_at = created_at for consistency
UPDATE tablos 
SET updated_at = created_at 
WHERE updated_at IS NULL;

-- Create function to update updated_at timestamp for tablos
CREATE OR REPLACE FUNCTION update_tablos_updated_at()
RETURNS TRIGGER AS $$
BEGIN
    NEW.updated_at = CURRENT_TIMESTAMP;
    RETURN NEW;
END;
$$ language 'plpgsql';

-- Create trigger to automatically update updated_at on tablos
CREATE TRIGGER update_tablos_updated_at
    BEFORE UPDATE ON tablos
    FOR EACH ROW
    EXECUTE FUNCTION update_tablos_updated_at();

-- Add comment to document the column
COMMENT ON COLUMN tablos.updated_at IS 
    'Timestamp when the tablo was last updated (auto-updated by trigger)';

